简体   繁体   中英

How to handle screen orientation change

I'm trying to handle screen orientation change for my android application but without success. Here's my manifest :

  <activity
        android:name="fr.ups.l3info.l3info_catchgameactivity.CatchGameActivity"
        android:label="@string/app_name" 
        android:screenOrientation="user"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and I adde this function in my activity class :

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

But when I change the screen orientation the application is recreated and this function is never executed. What I have done wrong? Thanks for your help.

在清单清单android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"使用此android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"

You might want to read more about activity lifecycle. More you can find here ( http://developer.android.com/training/basics/activity-lifecycle/index.html )

But more important is to get familiar with your activity state .

Method you are looking for is not onConfigurationChanged(Configuration newConfig) , but onSaveInstanceState(Bundle savedInstanceState) .

Now in your OnCreate() method you will need to check if there is some state already saved, and then recreate that situation.

There is a very good explanation on this link: Saving Android Activity state using Save Instance State

But basically what you need to do is this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and then with that in mind:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

STATE_SCORE and STATE_LEVEL are some public static final String values that are used to somehow label your stuff you want to save.

For example, if you have EditText view, and you type something in, then rotate your screen, that value will be lost. But in your onSaveInstanceState() method you shall use that Bundle parameter and put value of your EditText view as a String.

With that saved, now you can get that value in your onCreate() method and set the value of your EditText view to it.

Hope this helps :)

If you're trying to define a "different layout" to your app when the orientation changes, just define a new layout give it the same name as your other layout and put it under res/layout-land .

So that when your application recreates itself and calls setContentView(layout_name) within the onCreate function it will call the under res/layout-land and not the one under res/layout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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