简体   繁体   中英

Wrong Activity launched, when launching application from recent task

I am creating an application where I am receiving notification.

On open of that notification, I am launching activity (created with flag FLAG_ACTIVITY_NEW_TASK ) on pressing back key, I am launching main application activity (with Flag FLAG_ACTIVITY_CLEAR_TOP ).

Now again pressing back key (for closing the application).

Problem is: when I go to recent task and open that application, notification activity launches, whereas main activity of application should launch.

Can anyone give me any suggestion on it?

added below snippet when i received notification in my BroadcastReceiver extended class -

Intent new_intent = new Intent(context, NotificationActivity.class);
new_intent.putExtra(NotificationActivity.EXTRA_NOTIFICATION_ID, id);
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(new_intent);  

on back key of NotificationActivity i am launching main activity -

Intent new_intent = new Intent(context, MainActivity.class);
new_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new_intent);

Now when i go back from application by back key and again starts application from recent task it show me NotificationActivity. while it should show MainActivity of application. is there any thing i am missing?

You should launch your MainActivity with these flags:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

after this don't forget to finish NotificationActivity

When your app is in killed state (not present in the recent task) your application back stack will be empty.

Now there is a Concept of Root (Activity) for your application stack. The 1st activity of your application that launches becomes the Root of the application back stack.

When you press back from your NotificationActivity and MainActivity both activity will be finished. Now if you launch the application from the recent task then the root activity will be launched from the Back Stack.

This is a normal android behavior which always launches the Root Activity from the back stack

Even in WhatsApp you will find the same behavior.

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM