Handling Runtime Changes
阅读:https://developer.android.com/guide/topics/resources/runtime-changes.html
前言不再累述,就表达一种情况:有可能在Activity restart的时候,有些数据通过系统自带的onRestoreInstanceState()会有很糟糕的效果并且代价很高,因此你有两种选择:
- 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.
Retain an object during a configuration change
If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the
Bundle
that the system saves for you with theonSaveInstanceState()
callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a statefulObject
when your activity is restarted due to a configuration change.
restarting你的ACtivity可能会有一些麻烦,比如一些网络连接的处理等等,onSaveInstanceState()也无法处理一些大型对象,比如bitmaps等等。
那么,有以下的方法:
To retain an object during a runtime configuration change:
- Override the
onRetainNonConfigurationInstance()
method to return the object you would like to retain.- When your activity is created again, call
getLastNonConfigurationInstance()
to recover your object.When the Android system shuts down your activity due to a configuration change, it calls
onRetainNonConfigurationInstance()
between theonStop()
andonDestroy()
callbacks. In your implementation ofonRetainNonConfigurationInstance()
, you can return anyObject
that you need in order to efficiently restore your state after the configuration change.
使用onRetainNonConfigurationInstance()和getLastNonConfigurationInstance(),onRetainNonConfigurationInstance会在onStop和onDestroy之间执行,我们可以在这里保存对象(在该方法返回对象)。
Caution: While you can return any object, you should never pass an object that is tied to the
Activity
, such as aDrawable
, anAdapter
, aView
or any other object that's associated with aContext
. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)
注意:返回对象的时候,对象不能与Activity有所联系,例如a Drawable, an Adapter, a View,不然就会造成泄露。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance(); if (data == null) { data = loadMyData(); } ... }
返回使用上面的getLastNonConfigurationInstance方法。
另外,如果你不想系统restart你的东西,可以使用Handle the configuration change yourself,自定义操作的程度高,但是也很有难度,有兴趣的可以自行前往当页了解。