Android: Dialog etc restore after rotation changed

久未见 提交于 2019-11-30 20:05:24

The approach I took was to not allow the OS to restart your activity after the layout configuration change. To accomplish this, add this line within activities that you want to prevent from restarting in your manifest file:

 <activity
 android:configChanges="orientation|keyboard"
 ...
 >

Optionally, you can handle the configuration change in code in case there are some layout changes you want to make manually, such as reloading a new view from XML. This is done by overwriting the onConfigurationChanged() method in your Activity class:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    //Handle config changes here, paying attention to
    //the newConfig.orientation value
    super.onConfigurationChanged(newConfig);
}

Edit: Added "|keyboard" to the list of config changes

Add like this in your activity tag in manifest

<activity android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:name=".your.package"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!