androidandroid-intentandroid-notificationsandroid-pendingintenttaskstackbuilder

Start new activity from notification in existing task


My app receives pushes and opens different activities according to the push type.

I use TaskStackBuilder for a pending Intent to create a synthetic backstack in conjunction with android:parentActivityNamein my manifest.

So far, so easy. When the App is not started, all works as expected. But if the app is in background (task is running), the pending Intent also starts my desired activity with the defined parent from the manifest, but resets the existing task. The problem is that other activities that were started by the user in the meantime are also cleared. enter image description here

So what a want to achieve is:

  1. if the app is not started, open the desired activity with the synthetic backstack (MainActivity)
  2. if the app is running, respect the current task order and just push the desired activity on top of it.

I can't seem to make it work with the TaskStackBuilder.

I'd be happy for some insights.


Solution

  • You can't really do this with TaskStackBuilder. It isn't designed for that. It always resets the task to begin with.

    I would do the following: