简体   繁体   中英

Activity still recreated with configChanges

As I understand it, if you insert 'android:configChanges="orientation"' into the activity in the manifest, the activity will not be destroyed and recreated on an orientation change. To test this, I created a dead-simple app which does NOTHING. Then I inserted 'android:configChanges="orientation"' in the manifest. Then I added the following method:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.v(TAG,"onConfigurationChanged:");
    super.onConfigurationChanged(newConfig);
}

However, I'm still seeing onCreate() being called. The activity is still being recreated.

As if that weren't strange enough, I don't see onConfigurationChanged() being called when I put the emulator into landscape mode (Ctrl-F11). It's only called when I go back to portrait mode. Shouldn't it be called both ways? Isn't the configuration (orientation) being changed when I go into landscape as well as portrait modes? This makes no sense.

Anyway, this whole activity and orientation thing is driving me crazy.

Your screen size changes from 1200x800 to 800x1200 (for instance).

Since API 13, this will also raise a screenSize config change. The fix is:

android:configChanges="keyboardHidden|oritentation|screenSize"

However, I'm still seeing onCreate() being called. The activity is still being recreated.

The emulator emulates a device with a side-slider keyboard. The android:configChanges value that matches your - would be keyboardHidden , generally used in conjunction with orientation to handle non-keyboard devices (eg, android:configChanges="keyboardHidden|orientation" ).

That being said, android:configChanges is not recommended in most cases. Use dynamic fragments and setRetainInstance(true) , or use onSaveInstanceState() and onRetainNonConfigurationInstance() to allow the activity to be destroyed and recreated.

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