Extras - yDelouis/androidannotations GitHub Wiki

Since AndroidAnnotations 1.0

@Extra

The @Extra annotation indicates that an activity field should be injected with the corresponding Extra from the Intent that was used to start the activity.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @Extra("myStringExtra")
  String myMessage;
	
  @Extra("myDateExtra")
  Date myDateExtraWithDefaultValue = new Date();

}

Since AndroidAnnotations 2.6

If you do not provide any value for the @Extra annotation, the name of the field will be used.

@EActivity
public class MyActivity extends Activity {

  // The name of the extra will be "myMessage"
  @Extra
  String myMessage;
}

Note that you can use the intent builder to pass extra values.

MyActivity_.intent().myMessage("hello").start() ;

Handling onNewIntent()

Since AndroidAnnotations 2.6

AndroidAnnotations overrides setIntent(), and automatically reinjects the extras based on the given Intent when you call setIntent().

This allows you to automatically reinject the extras by calling setIntent() from onNewIntent().

@EActivity
public class MyActivity extends Activity {

    @Extra("myStringExtra")
    String myMessage;

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }
}

Executing code after extras injection

Since AndroidAnnotations 3.1

If you need to execute code after extras injection, you should use the @AfterExtras annotation on some methods.

@EActivity
public class MyClass {

  @Extra
  String someExtra;

  @Extra
  int anotherExtra;


  @AfterExtras
  public void doSomethingAfterExtrasInjection() {
    // someExtra and anotherExtra are set to the value contained in the incoming intent
    // if an intent does not contain one of the extra values the field remains unchanged
  }

}

Note : If the parent and child classes have @AfterViews, @AfterInject or @AfterExtras annotated methods with the same name, the generated code will be buggy. See issue #591 for more details.