0
votes

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.

1
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
your solution is similar to Sergio Feo's answer; please see the comment to the answer. - Mike Spike

1 Answers

0
votes

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.