124697
124697

Reputation: 21893

Which intent flag do I need to use to relaunch an activity

I need the calling activity to relaunch a child. that is, if the child activity already exist, close it then launch it again. and keeping the history/relationship between the parent and child. ie when I press back from the child I want it to go to the parent

CLEAR_TOP says that it will not relaunch an activity if it already exists

NEW_TASK sounds like it will make the child activity the root of the app which I don't want

Upvotes: 0

Views: 1174

Answers (2)

vliux
vliux

Reputation: 97

According to my understanding you have 3 requirements:
1. There is only one instance of child activity:
I don't know the reason, but if you just want to ensure the singleton of child activity on current task top, then singleTop is fine; otherwise you need to use singleTask, but then child activity will be created at the root of another task.
2. If the child already exists, close it then launch it again:
For both singleTop and singleTask, if there is already an instance of child activity (for singleTop, already an instance on current task top), the intent will be delivered to that instance. You can reset your activity status in onNewInstance(). This should have the same effects of creating a new one.
3. When I press back from the child I want to go to the parent:
For singleTop, no need to make anymore effort.
For singleTask, you can try if this tricky works: If the child activity knows what is the next activity to show by pressing back, it can manually start the parent activity by calling startActivity() in onDestroy().

More information about Android lauchMode, read http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Upvotes: 1

marshallino16
marshallino16

Reputation: 2671

Use this, works for me

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                                             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                                                              getApplicationContext().startActivity(intent);

Upvotes: 0

Related Questions