2

How to get extras from intent, when i use flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT ? Is this possible ?

Intent i = new Intent(ctx, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("a", "abc");
startActivity(i);       

In MyActivity i get null:

@Override
protected void onResume() {
    super.onResume();
    Log.i(TAG, getIntent().getExtras()+""); // <- get null
}
marioosh
  • 27,328
  • 49
  • 143
  • 192

2 Answers2

4

override Activity.onNewIntent() method in MyActivity

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

    // something you want
}

normal onResume() lifecycle will follow after onNewIntent() is called.

Luvie
  • 126
  • 7
  • It works like a charm. Thank You very much. Relative to @Farouk answer I've checked that `setIntent(intent);` is not necessary. – marioosh Jul 24 '14 at 18:28
1

your first activity starts MyActivity using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. This means that if there is already an active (non-finished) instance of MyActivity in the task stack, this instance of the activity will simply be moved to the front (top of the activity stack). In this case, onCreate() will not be called on MyActivity, because it isn't recreating the activity. Instead onNewIntent() will be called. You have not overridden this method in MyActivity.

You should override onNewIntent() and extract your data from the extras that are passed in the Intent argument that you receive in onNewIntent().

Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25