AndroidAnnotations——Reinject NonConfiguration Instance重新注入NonConfiguration 实例

NonConfiguration Instance

Since AndroidAnnotations 2.5


When a configuration change occurs, your activities are usually destroyed and recreated. This behavior is great to reload resources, but you usually need to transfert references to extensive states (loaded bitmaps, network connections, actively running threads...) from the old to new activity instance.
当配置发生改变时,你的activity往往先销毁再重新生成。这样重新加载资源挺好的,但是你通常需要从老的activity实例转移大量状态(加载的位图,网络连接,正在运行的线程……)引用到新的activity实例中。

That's what  Activity.onRetainNonConfigurationInstance()) is for (see  RetainingAnObject). Using this method requires casting from  Object, and can be cumbersome when you have multiple objects.
那就是   Activity.onRetainNonConfigurationInstance() 的作用(查看   RetainingAnObject )。使用这个方法需要从   Object 类型强制转换,而且当你有多个对象时,这种方法比较累赘。

@NonConfigurationInstance


Annotate your activity fields with  @NonConfigurationInstance to retain instances that are intensive to compute, on configuration changes.
在你的activity字段上加注解就可以在配置改变时保留需要密集计算的实例。

public class MyActivity extends Activity {

  @NonConfigurationInstance
  Bitmap someBitmap;

  @NonConfigurationInstance
  @Bean
  MyBackgroundTask myBackgroundTask;

}
Caution: while you can annotate any field, you should never annotate a field 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 的字段上,比如 Drawable   Adapter View   或者其他与  Context 有联系的对象。

This caution doesn't apply to beans annotated with @Bean, because AndroidAnnotations automatically takes care of rebinding their context.这个警告不适用于加了 @Bean注解的beans,因为AA会自动照看好并重新绑定它们的context。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值