Android up navigation for an Activity with multiple parents

后端 未结 3 1416
眼角桃花
眼角桃花 2021-01-30 10:50

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

\"App

3条回答
  •  佛祖请我去吃肉
    2021-01-30 11:29

    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> parents = new Stack>();
    

    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 (:

提交回复
热议问题