I have a problem for implementing up navigation on an app with this navigation tree:

App navigation tree

The standard implementation of the back button is fine.

The problem start when trying to implement the Up button.

What I expect:

  • when the user is on Detail 5 Activity and press the up button the app goes to List 3 Activity
  • when the user is on Detail 7 Activity and press the up button the app goes back to Home Activity

So in different terms, I'd like to have this behaviour on the back stack:

app backstack clear

The Android documentation (Implementing Ancestral Navigation) advice to use the following code to handle up navigation:

Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();

But because the parent activity of the Detail Activity differs on the different navigation path I don't know which one it really is. So I can't call it in the Intent.

Is there a way to know the real parent activity in the Android back stack?

If not, is there a way to implement a correct up navigation in this app?

Edit
Report