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?
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.
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.
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);
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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With