Keeping data view/layout in Android after screen rotate/activity destroyed

独自空忆成欢 提交于 2019-11-29 08:42:28

Do you need your activity to be recreated? Is there work you want to do on rotation? savedInstanceState is where you would store data to be passed to the recreation of the Activity, but since you aren't using XML layouts you may consider just adding android:configChanges="orientation" to your activity in the manifest. This will give you manual control over what happens during rotation change.

In additional to my original comment, I would override config changes for keyboard being show as well:

android:configChanges="orientation|keyboardHidden"

If there is work you want to do on rotation change you can override onConfigChange and do anything you need there.

It's hard to say that there is a time when you should always override config changes, but I often do it when I have no dependency on resource folders that are size or orientation specific and I'm doing a lot of work that would take too much time to recreate.

There are other best practices to store data while activitiy config changes.

1. Developer Doc Handling Runtime Changes
2. Alex Lockwood : Handling Configuration Changes with Fragments, This will answer all your questions reagarding config change and storing data.

Impt: Fragment file to store data. You must set setRetainInstace(true) in order to store data and retrieve while your activity config changes.

@Override 
   public void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);        
          // retain this fragment
          setRetainInstance(true);    
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!