当前位置: 代码迷 >> 综合 >> AndroidAnnotations——Reinject NonConfiguration Instance重新注入NonConfiguration 实例
  详细解决方案

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

热度:12   发布时间:2023-12-12 00:00:27.0
87人阅读 评论(0) 收藏 举报
AndroidAnnotation

目录(?)[+]

  1. NonConfiguration Instance
    1. NonConfigurationInstance

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 {
        @NonConfigurationInstanceBitmap someBitmap;@NonConfigurationInstance@BeanMyBackgroundTask 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。

  相关解决方案