Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Intent() starts new instance with Android: launchMode="singleTop"

I have Activity A with android:launchMode="singleTop" in the manifest.

If I go to Activity B, C, and D there I have menu shortcuts to return to my applications root activity (A).

The code looks like this:

Intent myIntent = new Intent(getBaseContext(), MainActivity.class); startActivity(myIntent); 

However, instead of returning to the already existing instance A of my MainActivity.class it creates a new instance -> it goes to onCreate() instead of onNewIntent().

This is not the expected behavior, right?

like image 883
znq Avatar asked Mar 11 '10 11:03

znq


People also ask

What is Android launchMode singleTop?

singleTopIf an instance is not present on top of task then new instance will be created. Using this launch mode you can create multiple instance of the same activity in the same task or in different tasks only if the same instance does not already exist at the top of stack.

What is launchMode in Android manifest?

This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched. Numerous instances of the activity can be generated, and multiple instances of the activity can be assigned to the same or separate tasks.

How do I start an intent in Java?

This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);

How do you initialize an intent?

You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult().


1 Answers

This should do the trick.

<activity ... android:launchMode="singleTop" /> 

When you create an intent to start the app use:

Intent intent= new Intent(context, YourActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

This is that should be needed.

like image 69
Vidar Vestnes Avatar answered Nov 16 '22 01:11

Vidar Vestnes