【问题标题】:Ignore Orientation change in Android忽略Android中的方向变化
【发布时间】:2013-03-29 06:29:46
【问题描述】:

我想知道当方向改变时如何防止应用重新加载?

我有一个小型测试应用程序,它会在按下停止按钮之前播放音频。如果我将我的 Android 或模拟器从水平更改为纵向,则停止按钮不起作用。加载应用程序的第二次出现。无论方向是否已更改,我都希望第一次出现继续运行。

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" android:orientation="horizontal">


<LinearLayout

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_weight="0.64" android:orientation="horizontal">


<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Play" />


<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Stop" />


</LinearLayout>


</LinearLayout>

Test.java

import android.app.Activity;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class TestAppActivity extends Activity {

private Button btnDisplay;

private MediaPlayer mediaPlayer;


@Override

public void onCreate(Bundle savedInstanceState) 

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

mediaPlayer = MediaPlayer.create(this, R.raw.nut);

mediaPlayer.setLooping(true);


((Button)findViewById(R.id.button1)).setOnClickListener( new View.OnClickListener() {


public void onClick(View v) {

mediaPlayer = MediaPlayer.create(LoopBugActivity.this, R.raw.nut);

mediaPlayer.setLooping(true);

mediaPlayer.start(); 

}

} );



((Button)findViewById(R.id.button2)).setOnClickListener( new View.OnClickListener() {


public void onClick(View v) {

mediaPlayer.stop();

}

} );

}

}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我添加到清单中: 安卓:screenOrientation="风景" android:configChanges="keyboardHidden|orientation"

    到 Activity Java 文件。

    @Override public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);     
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
        } 
    

    布局保持横向,方向的旋转不会影响应用的播放。

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您的活动在方向更改期间被破坏并重新启动(在 Android 领域也称为配置更改)。

      在您的 AndroidManifest.xml 文件中,将以下内容添加到您的活动声明中:

      android:configChanges="orientation"
      

      所以它看起来像下面这样:

              <activity android:name="com.your.package.TestAppActivity"
              android:configChanges="orientation"
          />
      

      然后,您的 Activity 可以监听配置更改并手动响应。更改方向将不再自动重新启动活动。

      【讨论】:

        【解决方案3】:

        我仍然没有得到我想要的结果,当我旋转设备时内容仍然发生变化(仍然调用了 onCreate() 方法)。 我给一个empty配置对象时实现的实际效果

        new Configuration() 而不是提供的参数 newConfig

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(new Configuration());
        }
        

        希望这对想要完全忽略方向变化事件的人有所帮助

        【讨论】:

          【解决方案4】:

          这就是 Android Activity 的设计方式。如果方向改变,它将重新加载新活动。要么禁用方向,要么将应用程序分为 2 层(前端和后端)。前端将仅显示进度和控制按钮,后端将从前端获取命令并执行。在此模型中,您将不会遇到现在面临的问题。

          看看这个link,它可能对你有帮助。

          【讨论】:

            【解决方案5】:

            在清单中,在您的标签中,输入 android:screenOrientation="portrait"

            http://developer.android.com/guide/topics/manifest/activity-element.html

            【讨论】:

              【解决方案6】:

              看看这篇文章:Handling Runtime Changes

              我认为最好处理更改,但您仍然可以选择阻止它,如“b. 自己处理配置更改”中所述。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2010-11-27
                • 2018-08-08
                • 1970-01-01
                • 1970-01-01
                • 2017-11-08
                • 2012-01-23
                • 1970-01-01
                相关资源
                最近更新 更多