2

My Android app is for a hiking club and starts with a Home screen, which is the main activity. The home screen shows the house-style and has some buttons to the core functionalities: list of upcoming hikes, history of hikes, last reactions and also a message board.

THe activity flow is pretty straightforward, you can i.e. navigate from: Home -> Hike list -> Hike Details

And back using the Back button. Going back from the Home activity will ask for closing the app. I already use the FLAG_ACTIVITY_CLEAR_TOP flag to prevent several instance of the same activity. But my problem is I have also implemented a Menu to navigate to the core functionalities directly.

So for example, when in the Hike Detail screen, one can choose to go to the Message board. But I do not want to keep the Hike List -> Hike Detail activities on the stack. So when pressing Back from the message board, I always want to return to the Home activity.

IS there a clean possibility to pop the stack and only keep the Home activity before launching a new activity? I guess that would solve my issue.

klausch
  • 612
  • 2
  • 10
  • 22

1 Answers1

2

I found something similar on SO that might work.

Check out: FLAG_ACTIVITY_TASK_ON_HOME

Pre API 11:

Start all of your activities from home using startActivityForResult(). When you navigate to a parallel stack via the global menu. Call this on your current activity:

// startParallelActivity();
setResult(KILL_YOURSELF);
finish;

Where every activity on top of home implements onActivityResult() like so:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == KILL_YOURSELF) {
            setResult(KILL_YOURSELF);
            finish();
        }
    }

This will destroy all the activities in the current stack, leaving just the home activity that will be there when the user hits "back"

Community
  • 1
  • 1
Johnny Z
  • 14,329
  • 4
  • 28
  • 35
  • I realize I just copied the post I linked to, so make sure you give him a plus 1 if it helps – Johnny Z Sep 20 '13 at 17:01
  • Sounds like a workable solution, it probably makes it necessary to create a new abstract subclass of Activity to act as the parent of all activities that should be run as the direct child activiy of the Home activity only, implementing tthe onActivityResult() method to remove any duplicate code. – klausch Sep 24 '13 at 19:34