3

I have successfully used androidannotations @Extra to decode an intent and get the sent message as this snippet demonstrates:

@Extra(MyActivity.MESSAGE)
String intentMessage;

@ViewById(displayMessage)
TextView textView;

@AfterViews
protected void init() {
    textView.setText(intentMessage);
}

I'm missing how, if possible, to create the intent in the first place using annotations. e.g replace the following

Intent intent = new Intent(this,DisplayMessageActivity_.class);
intent.putExtra(MESSAGE, s);
startActivity(intent);

with something. Is this possible? (I'm totally new to all this so probably am missing something terribly obvious)

SOLUTION:

DisplayMessageActivity_.intent(this).intentMessage(s).start();

Where intentMessage is the name of the extra field.

RichieHH
  • 2,116
  • 4
  • 28
  • 30

3 Answers3

7

Yes, you can use the following:

// Starting the activity
MyListActivity_.intent(context).start();

// Building an intent from the activity
Intent intent = MyListActivity_.intent(context).get();

// You can provide flags
MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();

// You can even provide extras defined with @Extra in the activity
MyListActivity_.intent(context).myDateExtra(someDate).start();

// startActivityForResult() equivalent:
MyListActivity_.intent(context).startForResult();

Source: https://github.com/excilys/androidannotations/wiki/HowItWorks

BVB
  • 5,380
  • 8
  • 41
  • 62
  • Can you possibly hold my hand a little here because I'm not seeing it. I read this before but I'm missing how that addresses my issue of setting the extra for EXTRA_MESSAGE - this is just a list of APIs from which I dont see how I set the string associated with the extra name (EXTRA_MESSAGE above) and then to invoke StartActivity. – RichieHH Mar 13 '14 at 00:30
  • Sorry, I have actually not used the @Extra annotation, but am simply aware of its existence. I am glad that you were able to figure it out! – BVB Mar 13 '14 at 03:31
  • Possibly you could accept my answer below to close this up? – RichieHH Mar 13 '14 at 21:49
  • 1
    I cannot do that as I am not the asker. I believe you are :) – BVB Mar 13 '14 at 22:06
1

Solution. Finally saw the pattern on how it works. Thanks.

DisplayMessageActivity_.intent(this).intentMessage(s).start();

where intentMessage is the @Extra defined in the activity to be started e.g

@EActivity(R.layout.activity_display)
public class DisplayMessageActivity extends Activity {

    public static final String MESSAGE = "net.richardriley.MyFirst.MESSAGE";

    @Extra(MESSAGE)
    String intentMessage;

    @ViewById(displayMessage)
    TextView textView;

    @AfterViews
    protected void init() {
        textView.setText(intentMessage);
    }

}
RichieHH
  • 2,116
  • 4
  • 28
  • 30
0

I know this is a late answer but I have developed a library that does exactly this. It uses annotations to generate the intent code.

Check it out in here: https://github.com/kostasdrakonakis/android_navigator

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60