0

Suppose there are 3 activities:

  1. MainActivity
  2. ActivityA
  3. ActivityB

The usage scenario is as follows:
-> MainActivity is opened then
-> it opens ActivityA then
-> ActivityA opens ActivityB then
-> user navigates backwards and when MainActivity is closed a notification is created for ActivityB

Now, when user clicks notification, it sends user directly to ActivityB where, if navigating backward it would go to the last opened activity before ActivityB, which in this case would be ActivityA -> MainActivity.

If MainActivity opens directly ActivityB(and navigating backwards as described above), then when notification is clicked it would send again to ActivityB but this time, when navigating backwards, the last activity before B would be MainActivity, so it should open the MainActivity and not as above(ActivityA -> MainActivity).

I would like to know what to search for and study and(eventually) how to accomplish this behaviour.

Thanks in advance.

Mike Spike
  • 389
  • 8
  • 20
  • put some flags and pass it with intents and check if you have got intent from "MainActivity" or from "ActivityA" and than on back pressed perfrom intent again according to the flag that you got from getIntent().getStringExtra("fromMainActivity") or from getIntent.getStringExtra("fromActivityA") – Jigar Sep 13 '14 at 09:48
  • your solution is similar to Sergio Feo's answer; please see the comment to the answer. – Mike Spike Sep 15 '14 at 08:45

1 Answers1

0

The cleanest solution would be to provide appropriate arguments to your activities via the Intent and evaluate them when reacting to onBackPressed. This way you can differentiate when an activity was started by normal navigation or via the notification.

EDIT: What you want is to actually make sure that, when an activity is already being displayed and a new one is opened via notification, it gets put on the back stack. I think this should be handled in OnNewIntent for the activity being called, so it knows whether it was started from normal navigation or from a notification. You achieve this by setting some flags in the Intent extras as mentioned in the original answer above. How that could be concretely achieved should be along the lines of this blog post. Minus the notification part.

Maybe this question can also shed light on how to determine the status of your application before setting the appropriate flags on your Intent.

Community
  • 1
  • 1
Sergio Feo
  • 292
  • 2
  • 7
  • I don't see yet how it would work for this case; basically, what I need is to save the back stack and navigate through activities in their last order, no matter where B was accessed from. – Mike Spike Sep 15 '14 at 08:44
  • True, what you want is to actually make sure that, when an activity is already being displayed and a new one is opened via notification, it gets put on the back stack. I think this should be handled in `OnNewIntent` for the activity being called, so it knows whether it was started from normal navigation or from a notification. You achieve this by setting some flags in the Intent extras as mentioned in the answer above. How that could be concretely achieved should be along the lines of: http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html – Sergio Feo Sep 19 '14 at 15:35
  • Sergio, thanks a lot for your time! The behaviour you described is almost what I need, except the activities component in the way: I have an app with, say, 5 activities and for one of them, let it be 3rd(it must be a deep activity), I need to put a notification when user leaves it. So no matter how I leave that activity(and/or app) - by pressing Back 'til the launcher or by pressing Home - I want to keep the backstack of how I reached that 3rd activity so that when user presses notification it sends the user to the 3rd activity where from, if again pressing Back, it'll send user through those – Mike Spike Sep 22 '14 at 06:32