App运行时的状态改变
》Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running Activity
(onDestroy()
is called, followed byonCreate()
). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
》However, you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience. In such a situation, you have two other options:
- Retain an object during a configuration change
Allow your activity to restart when a configuration changes, but carry a stateful object to the new instance of your activity.
- Handle the configuration change yourself
Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary.
》You can add such fragments to your activity to preserve stateful objects.
To retain stateful objects in a fragment during a runtime configuration change:
- Extend the
Fragment
class and declare references to your stateful objects. - Call
setRetainInstance(boolean)
when the fragment is created. - Add the fragment to your activity.
- Use
FragmentManager
to retrieve the fragment when the activity is restarted.
For example, define your fragment as follows:
public class RetainedFragment extends Fragment { // data object we want to retain private MyDataObject data; // this method is only called once for this fragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // retain this fragment setRetainInstance(true); } public void setData(MyDataObject data) { this.data = data; } public MyDataObject getData() { return data; } }
》Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion
andtargetSdkVersion
attributes), you must include the "screenSize"
value in addition to the"orientation"
value. That is, you must decalare android:configChanges="orientation|screenSize"
. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
》Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged()
.