Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Activity B from Notification and force back button to run Activity A

I have an Android application that contains two Activities.

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

There is also a Notification that opens Activity in the same way.

If I start B from this notification and press back button - it just closes B and does not shows A like I go there with normal case.

Is it possible to force B to bo back to A if started from notification without history stack ?

Solution

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

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

android:launchMode="singleTask"

there will be in A activity called

onNewIntent(Intent intent)

And there we should checl if Intent containst extra value passed from notification - and if so, then we call new B intent.

Thank you both and good luck with it for next devs ;-)

like image 794
hsz Avatar asked Feb 23 '23 04:02

hsz


1 Answers

I would suggest having the notification call Activity A (instead of B directly) with some flag in its extras bundle. In A's onCreate(), check for the flag, and immediately launch Activity B. This will ensure that pressing back on B will return to A.

like image 96
Paul Lammertsma Avatar answered Apr 06 '23 14:04

Paul Lammertsma