Android screen orientation data lose prevention

微笑、不失礼 提交于 2019-12-06 06:30:43

On your Activity in your AndroidManifest, set the following:

<activity android:name="YourActivity" android:configChanges="orientation|keyboardHidden"></activity>

This tells your Activity to not "re-create" itself on screen orientation changes or keyboard change state

You can save the data by using :

@Override
public Object onRetainNonConfigurationInstance() {
    return super.onRetainNonConfigurationInstance();
}

return the data you want to save.

In the onCreate function you can check if there is any data saved by doing the following

final Object data = (Object) getLastNonConfigurationInstance());
    if (data == null) {
        //Retrieve data
    }else{
        //Use the saved data
        settings = data;
        }

You can read about this topic at the Android Dev Guide :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!