Handling Runtime Changes

阅读:https://developer.android.com/guide/topics/resources/runtime-changes.html

 

前言不再累述,就表达一种情况:有可能在Activity restart的时候,有些数据通过系统自带的onRestoreInstanceState()会有很糟糕的效果并且代价很高,因此你有两种选择:

  1. 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.

  2. 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 the onSaveInstanceState() 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 stateful Object when your activity is restarted due to a configuration change.

  restarting你的ACtivity可能会有一些麻烦,比如一些网络连接的处理等等,onSaveInstanceState()也无法处理一些大型对象,比如bitmaps等等。

  那么,有以下的方法:

To retain an object during a runtime configuration change:

  1. Override the onRetainNonConfigurationInstance() method to return the object you would like to retain.
  2. 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 the onStop() and onDestroy() callbacks. In your implementation of onRetainNonConfigurationInstance(), you can return any Object 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 a Drawable, an Adapter, a View or any other object that's associated with a Context. 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,自定义操作的程度高,但是也很有难度,有兴趣的可以自行前往当页了解。

 

 

 

 

 

转载于:https://www.cnblogs.com/yutoulck/p/3402529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值