方向重置活动Android

时间:2015-03-26 06:25:55

标签: android orientation reset

每次关闭手机横向模式时,我的活动都会重置。 如何停止重置活动?

这是我尝试使用的代码,但它不起作用。

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

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.get_msg);

    } else {
        setContentView(R.layout.get_msg);
    }
}

清单文件:

android:configChanges="orientation|keyboardHidden|screenSize"

以下是我从中获取的链接:

prevent activity restarting when orientation changes

3 个答案:

答案 0 :(得分:3)

更改此

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            System.out.println("this's landscape");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            System.out.println("this's portrait");
        }
    }

答案 1 :(得分:1)

设置android:configChanges =" orientation | keyboardHidden | screenSize"在androidMainifest.xml中会阻止活动重置,只是onConfigurationChanged()会执行,你的引用是处理支持两个布局的东西,它不适合你的application.onConfigurationChanged()你在这里重新加载布局,所以活动没有重置,只是你的布局重置。

答案 2 :(得分:0)

另一种方式:

String text;
@Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  int rotation = getWindowManager().getDefaultDisplay().getRotation();
        if(rotation == 0)
            text="Portrait";
        else
            text="Landscape";
}