Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start activity with populated backstack?

I have a notification that needs to start a activity B. However, I want to be able to let the user "go back" to the Dasboard A.

I am wondering how this could be done? Will I need to use a intent extra to start A and process that intent in onCreate and then start B manually? Or is it possible to manually specify a backstack?

like image 659
Peterdk Avatar asked Jan 30 '12 12:01

Peterdk


1 Answers

You can use the startActivities method to launch a complete backstack of activities in one go.

startActivities(
    new Intent[]
    {
        new Intent("my.intent.FOO_INTENT"),
        new Intent("my.intent.BAR_INTENT"),
        new Intent("my.intent.BAZ_INTENT")
    });

In this example an instance of Baz is created and made the current Activity.

If Baz finishes then an instance of Bar is created and is made the current Activity.

If Bar finishes then an instance of Foo is created and is made the current Activity.

This method was introduced in API level 16 but is available in the v4 support library via the ContextCompat class:

ContextCompat.startActivities(context,
    new Intent[]
    {
        new Intent("my.intent.FOO_INTENT"),
        new Intent("my.intent.BAR_INTENT"),
        new Intent("my.intent.BAZ_INTENT")
    });
like image 94
Dean Wild Avatar answered Nov 06 '22 06:11

Dean Wild