Launch "Activity B" from the "Notification" and "Force back" buttons to launch "Activity A"

I have an Android app containing two Activities .

Activity A has a button that launches Activity B using Context.startActivity(Intent intent) .

There is also Notification , which opens an Activity in the same way.

If I run B from this notification and press the "Back" button, it just closes B and does not show A how I go there with a normal case.

Is it possible to get B to return to A if it started with a notification without a history stack?

Decision

As stefan and Paul Lammertsma, the best way is to start A from the notification and in A create a new intent using B - but not in onCreate() !

I dig a bit and find that if I set a new property for activity A in AndroidManifest :

 android:launchMode="singleTask" 

in A will be an activity called

 onNewIntent(Intent intent) 

And there we have to checl, if the Intent contains an additional value passed from the notification, and if so, then we invoke the new intent B

Thank you both and good luck with it for the following developers :-)

+4
source share
3 answers

I would suggest that Activity A be called (instead of B directly) with a notification with some flag in its package of additional services. In A onCreate() check the flag and immediately start Activity B. This will ensure that B returns to A.

+4
source

An easy way to achieve this would be to actually launch Activity A from your Notification with a flag to instantly call Activity B.

Thus, you just need to add additional information about your intention, which you start in your notification, and you need to check action A, if it additionally exists, and if it exists, then you start action B.

Update: another way, but, in my opinion, not very good, would be to override the onPause () method of your action B and call Activity A there.

+3
source

Maybe not the most beautiful solution, but nonetheless quick; add an extra value to run B, "Running FromNotification" or something like that.
In the Bs onCreate () operation, you store this boolean for later use.
In the Bs onBackPressed () action, you can check the value of your boolean and, if true, start the action A before calling finish ();

A better solution would be to start activity A from a notification, with additional information that it should start activity B directly.

+1
source

Source: https://habr.com/ru/post/1385092/


All Articles