Extras - WonderCsabo/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;
}

Method based injection

Since AndroidAnnotations 4.0.0

@EActivity
public class MyActivity extends Activity {

  @Extra("myStringExtra")
  void setOneExtra(String myMessage){
    // do something with myMessage
  }
  
   void setMultipleExtrass(@Extra String myMessage, @Extra("myDateExtra") Date myDateExtraWithDefaultValue){
    // do something with myMessage and myDateExtraWithDefaultValue
  }

}

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) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}

Since AndroidAnnotations 3.2

When a method annotated with @AfterExtras is present in the activity, we override the onNewIntent() method to call setIntent().

Since AndroidAnnotations 4.0.0

WARNING: Starting with Android Annotations 4.0.0 we no longer override onNewIntent() and you have to override it by yourself if needed.

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
  }

}

Warning

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.

Also, while there is a guaranteed order about when we call @AfterViews, -Inject or -Extras annotated methods, there is no guaranteed order for calling each of the methods with the same @AfterXXX annotation (see issue #810).

Details about when the methods with one of those annotations are called you can find here.