Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

NonConfigurationInstance

Csaba Kozák edited this page Sep 1, 2014 · 3 revisions

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.

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.

@NonConfigurationInstance

Annotate your activity fields with @NonConfigurationInstance to retain instances that are intensive to compute, on configuration changes.

@EActivity
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.

This caution doesn't apply to beans annotated with @Bean, because AndroidAnnotations automatically takes care of rebinding their context.

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally