Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android up navigation for an Activity with multiple parents

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?

like image 797
ol_v_er Avatar asked Feb 01 '13 20:02

ol_v_er


People also ask

How do you link two activities together?

this, ToActivity. class); startActivity(i); In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter. Make sure that you add your second Activity to the manifest also (it resides in the tag)!

How do I set parent activity on android?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

What is the difference between the up button and back button?

When you press the Back button, the current destination is popped off the top of the back stack, and you then navigate to the previous destination. The Up button appears in the app bar at the top of the screen.


2 Answers

I will stick with my comment on Paul's answer:

The idea is to have a Stack of the last Parent Activities traversed. Example:

public static Stack<Class<?>> parents = new Stack<Class<?>>(); 

Now in all your parent activities (the activities that are considered parents -e.g. in your case: List and Home), you add this to their onCreate:

protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      parents.push(getClass());       //or better yet parents.push(getIntent()); as @jpardogo pointed      //of course change the other codes to make use of the Intent saved.       //... rest of your code } 

When you want to return to the Parent activity, you can use the following (according to your code):

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

I hope am right (:

like image 90
Sherif elKhatib Avatar answered Oct 19 '22 10:10

Sherif elKhatib


That's a tricky question and in my opinion really shows the difficulties in coping with the UX decisions of Android for the "up button". Therefore, there's not a clear-cut answer to your problem.

I have two possible solutions for you.

1. Mimicking the back button behavior.

You could consider adding an extra to the intent for launching Detail from one of its various parents. This extra would inform those activities which activity they would need to launch when android.R.id.home is pressed.

This would effectively mean that your app "goes back" to its common ancestor, instead of simply relaunching Home.

Another way of implementing this may be simply executing onBackPressed() instead of launching Home with Intent.FLAG_ACTIVITY_CLEAR_TOP, but bear in mind that the associated animation would be different than a normal "up" action.

2. Skip intermediate activites and go home.

Some apps treat the "up button" as a "home button". You might want to consider having it simply always relaunch Home with Intent.FLAG_ACTIVITY_CLEAR_TOP.

like image 37
Paul Lammertsma Avatar answered Oct 19 '22 09:10

Paul Lammertsma