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:parentActivityName
in 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.
So what a want to achieve is:
- if the app is not started, open the desired activity with the synthetic backstack (MainActivity)
- 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.
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:
Activity
. Don't useTaskStackBuilder
and don't create any artificial back-stack. ThisActivity
will run in the application's current task if the application is currently active, and it will be put on top of the most recentActivity
that is open.onCreate()
of this newActivity
, check if thisActivity
is the root of the task usingisTaskRoot()
. If thisActivity
is the root of the task, this means that the app was not active prior to launching thisActivity
. In this case, you can create an artificial back-stack usingTaskStackBuilder
and launch the thing again the way you want it (this will reset the task).