Question

In my Android application I'm trying to make sure that certain activity should be always placed at the root of backstack when navigating to it.

For example user starts A->B->C->D activities. Imaging that A - splash screen (with NoHistory = true), B - main page, C,D - some details pages. On details page (D) user has ability to go to the main activity (B). Having B-C-D backstack User press "Go To B" button and navigated to the existing home activity (B), same time C,D activities are stopped and destroyed. I'm using SingleInstance for my B activity and it works fine in this case.

I have case when this doesn't work for me. At the splash screen I can discover that I should skip main page and go directly to one of the details page (for example D). And here when I go to home page my backstack is wrong: D->B instead of just B.

What flags or attributes are more suitable in my case. May be it is more valid approach to update application navigation logic?

Thank you for any suggestions!

Was it helpful?

Solution

In A:

Intent intent = new Intent(B.class, this);
startActivity(intent);
finish(); //finish A behind you so you can only explicitly return to it

In B, two methods for the buttons, I guess, that get you to either C or D:

public void buttonToC(View view){
    Intent intent = new Intent(C.class, this);
    startActivity(intent);
}

public void buttonToD(View view){
    Intent intent = new Intent(D.class, this);
    startActivity(intent);
}

In C and D, you can optionally finish the Activity if you press back:

public void onBackPressed(){
    Intent intent = new Intent(B.class, this);
    startActivity(intent);
    finish(); // optional
}

So A -> B and A finishes. From B -> C OR D but B is still running in the background. When in C or D, pressing back brings up the background activity, B. This should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top