Question

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.

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?

Était-ce utile?

La solution

It is a normal way. But it is long - 5 sec of waiting.

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.

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

    SetupActivity();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top