关闭

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

标签: AndroidAnnotation
787人阅读 评论(0) 收藏 举报
分类:

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 theActivity, 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 AdapterView 或者其他与 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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66918次
    • 积分:1188
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:7篇
    • 译文:33篇
    • 评论:16条
    最新评论