36

I have an activity, which is a singleTask:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        System.out.printf("### MyAcitvity.onCreate: " + getIntent().getExtras());
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("### MyActivity.onResume: " + getIntent().getExtras());
    }

    public void toNext(View v) {
        startActivity(new Intent(this, AaaActivity.class));
    }
}

and:

<activity android:name=".MyActivity" android:launchMode="singleTask" />

No it has been started, and it started another activity called AaaActivity.

public class AaaActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aaa);
    }

    public void jumpToFirst(View v) {
        Intent intent = new Intent(this, MyActivity.class);
        intent.putExtra("aaa","bbb");
        startActivity(intent);
    }

}

In the later one, there is a button which will trigger the jumpToFirst method to start the first one, and pass a data pair aaa=bbb. Since MyActivity is singleTask, it won't create a new one, instead, it will just back to the existing MyActivity instance.

My question is: how to get the passing extra data from AaaActivity in MyActivity?

You can see I have logged onCreate and onResume methods, but it prints null extra data.

11-16 08:44:17.833: INFO/System.out(5171): ### MyAcitvity.onCreate: null
11-16 08:44:19.417: INFO/System.out(5171): ### MyActivity.onResume: null
11-16 08:44:26.041: INFO/System.out(5171): ### MyActivity.onResume: null
11-16 08:44:28.897: INFO/System.out(5171): ### MyActivity.onResume: null

2 Answers 2

116

Try setting the launchMode type of your MyActivity as singleTop and then override the following method to look for the new intent:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    //now getIntent() should always return the last received intent
}
6
  • 23
    This solution also worked for me using a launchMode of singleTask.
    – daveywc
    Mar 19, 2013 at 6:13
  • with singleTask the extra that i'm passing is received with different values.
    – Maxrunner
    Oct 22, 2013 at 12:53
  • 2
    This really saved my day! My problem was: i opened other app through button in my app and then i really had problems to receive the intent content from this sharing app - there was just no reaction at all. Thx for this solution.
    – kosemuckel
    Nov 9, 2015 at 7:42
  • 2
    it works with launchMode of singleInstance also. Thanks. Nov 28, 2016 at 14:33
  • 1
    Big thanks, and when start activity using FLAG_ACTIVITY_SINGLE_TOP , activity's launching path is onNewIntent --> onStart --> onResume
    – KaKa
    Dec 22, 2016 at 2:52
1

If you must have single Task then use these flags for your intent:

 resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);

if you use PendingIntent so also: PendingIntent.FLAG_CANCEL_CURRENT :

pIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT ); 

(also use onNewIntent in your activity to handle the extras.)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.