Android for multi-parent activity navigation

I had a problem with embedding up navigation in an application with this navigation tree:

App navigation tree

The standard implementation of the back button is fine.

Problem while trying to implement the Up button.

What I expect:

  • when the user is on Details 5 Activities and press the button up, the application goes to List 3 Activities
  • when the user is on Details 7 Activities and press the button up, the application will return to Home>

So, in different expressions, I would like to have this behavior in the back stack:

app backstack clear

Documentation for Android ( Embedding Ancestral Navigation ) to use the following code to handle upward navigation :

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

But since the parent activity of the Detailed action is different from the different navigation path, I don’t know what it really is. Therefore, I cannot call it intent.

Is there a way to find out the real parental activity on the Android stack back?

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

+34
android android-actionbar
Feb 01 '13 at 20:22
source share
3 answers

I will stick to my comment on Paul:

The idea is to have a stack of recent Parent actions taken. Example:

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

Now in all of your parent actions (actions that are considered to be parents - for example, in your case: "List and home"), you add this to your 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 } 

If you want to return to 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 that I am right (:

+22
Feb 01 '13 at 22:52
source share

This is a difficult question and, in my opinion, really shows the difficulties in solving the Android UX solutions for the up button. Therefore, there is no clear answer to your problem.

I have two possible solutions for you.

1. By pressing the back button.

You might consider adding an additional launch intent to Detail from one of its different parents. This will additionally inform about the types of activities that they will have to start when you click android.R.id.home .

This would actually mean that your application would β€œrevert” to its common ancestor, instead of simply restarting Home.

Another way to implement this could be to simply execute onBackPressed() instead of starting Home with Intent.FLAG_ACTIVITY_CLEAR_TOP , but keep in mind that the related animation will be different from the usual up action.

2. Skip the intermediate steps and go home.

Some applications treat the up button as a home button. You might think that it just always restarts Home with Intent.FLAG_ACTIVITY_CLEAR_TOP .

+2
Feb 01 '13 at 20:38
source share

This is an old post for sure, but when studying SharedPreferences, I think it might be possible to collect this information into sharedPreferences and change its value each time before going down to 2 parents. Then, after reading it, you should be able to directly know your parent, and this without the need to create an entire class for him.

0
Nov 17 '16 at 15:49
source share



All Articles