I have two activites. Activity A
and Activity B
.
Activity A
is a singleTask activity. Activity B
has no extra configuration. So it is a normal activity.
When Activity A
launches the activity B
then if user presses to the home button then comes back immediately (so my app is not killed by android yet.) My app is showing activity A
. But it should show the activity B
because the user pressed to home button when active activity is B.
If i remove the singleTask property from Activity A. It is working as expected. But i have to use singleTask activity for some other reason.
How can i resolve this problem?
As you have constraint to use singleTask
launch mode, you can do following :
Home
, app or Activity B
goes in background (stopped state
)Activity A
because of singleTask
launch modeActivity A
, override onNewIntent()
method. This will get called from android os. You can fire Activity B
's intent from this method. So, system will open Activity A
when coming from background to your app, but from this method, Activity B
would be shown. I've edited my answer to handle below scenario :
ActivityA
, we press Home button and we come back, ActivityA
should be shown, not ActivityB
. To do this, I used boolean
shouldOpenB
in Activity A :
static boolean shouldOpenB = false;
// set shouldOpenB to true when you are opening ActivityB from your code with Intent
// reset shouldOpenB to false in onStart() of AcrivityA
protected void onNewIntent (Intent intent) {
if(shouldOpenB) {
Intent openIntentB = new Intent(this, ActivityB.class);
startActivity(openIntentB);
}
}
Note : The animation of Activity A
-> Activity B
would be visible to user for fraction of time. If you agree to change to singleTop
, we can remove this animation also.