3

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!

Mando
  • 11,414
  • 17
  • 86
  • 167

1 Answers1

2

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.

mike
  • 1,318
  • 3
  • 21
  • 41
  • navigating back from details activities (C,D) by calling finish() works fine. And as I see I can achieve my goal without any special attributes and flags which modifies default activity activation behavior. Unfortunately I'm using framework (MvvmCross) which uses default navigation approach and I want not to change logic but change attributes and flags of activity to achieve my goal. Is it possible? – Mando Oct 23 '13 at 20:00
  • 1
    Standard activity flow should be sufficient but see if http://developer.android.com/guide/components/tasks-and-back-stack.html is helpful. – mike Oct 23 '13 at 20:34