user1929742
user1929742

Reputation: 21

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?

Upvotes: 2

Views: 708

Answers (2)

Tamal Samui
Tamal Samui

Reputation: 738

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.

.

Upvotes: 0

Alex Zaitsev
Alex Zaitsev

Reputation: 1781

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

Upvotes: 2

Related Questions