简体   繁体   English

如何在Android上处理屏幕旋转

[英]How to handle screen rotation on Android

I have a ListActivity which I want to show when the phone's orientation is portrait and I have another normal Activity which I want to show when the user rotates the phone to landscape mode. 我有一个ListActivity,当手机的方向是纵向时,我想显示它;还有一个普通的Activity,当用户将手机旋转到横向模式时,我想显示它。

Some code: 一些代码:

  public class ListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT) {
            //Do stuff
        }else if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Intent myIntent = new Intent(getApplicationContext(), AnotherActivity.class);
            startActivity(myIntent);
        }
    }
}

This approach works, but has some problems. 这种方法有效,但存在一些问题。 Is there a more correct way? 有没有更正确的方法? The problem with this method is that when you rotate your phone and press the back button, the screen just becomes black since the phone is not rotated. 这种方法的问题在于,当您旋转手机并按返回按钮时,由于手机没有旋转,因此屏幕仅变黑。 So should I implement the rotation in another way, or modify the back button in some way so that the user cant return to the previous Activity? 因此,我应该以另一种方式实现旋转,还是以某种方式修改后退按钮,以使用户无法返回到上一个活动?

It is a normal way. 这是正常的方式。 But it is long - 5 sec of waiting. 但这很长-等待5秒。

There is another way: 还有另一种方法:

register orientation change as a configuration change 将方向更改注册为配置更改

A. 一种。

activity android:name=".SomeActivity" 
android:label="@string/app_name"
android:configChanges="orientation"

And process it as a config change. 并将其作为配置更改处理。

B. B.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);

    SetupActivity();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM