سؤال

In my Android project I have Activities A,B,C,D. I set in Manifest that A is parent of B. B is parent of C and C is parent of D. If i go: A->B->C->D and press back in Action Bar everything works fine. I go: A->C->D->B. If i press back in action bar of B, it takes me to A and not to activity D. Any ideas what to do?

هل كانت مفيدة؟

المحلول

It seems like you confuse back and up navigation: Back navigation gets you to the last activity on the back stack, up navigation gets you the navigation that you have defined as the parent activity:

Example:
A is the parent of B and B is the parent of C. (A -> B -> C)
You go from A -> C - > B

If you use the back button, you will got back to the activity that was open before the current activity. So you will land at C.
If you use the up navigation (by tapping the app icon on the upper left of the screen) you will go to the parent activity. In our case B.

Hope this helps and ask if you have further questions.

EDIT: If you want the up button behave like the back button you could do the following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        startIntent(new Intent(this, yourParentClass.class));
        return true;
    }
}

This way you would start a new activity.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top