32

The Android app design I'm working with calls for the "Up" button to behave the same way the "Back" button behaves, but I'm not sure how to make that happen.

I know that android:parentActivityName must be specified for the "Up" button to be visible on an activity, but specifying a fixed parent activity doesn't really make sense for the activity. Imagine this scenario with activities A, B, and C:

  1. Launch into activity A, it contains two buttons: each taking you to activities B and C, respectively.
  2. Tap the button for activity B.
  3. Transition to activity B. it contains two buttons: each taking you to activities A and C, respectively.
  4. Tap the button for activity C.
  5. Transition to activity C.
  6. Tap the "up" button, you should be taken to activity B.
  7. On activity B now: tap the button for activity A.
  8. Transition to activity A.
  9. Tap the "up" button, you should be taken to activity B.
  10. On activity B Tap the "up" button, you should be taken to activity A.
  11. On activity A now: tap the button for activity C.
  12. Transition to activity C.
  13. Tap the "up" button, you should be taken to activity A.

If I were to specify android:parentActivityName for each activity, it might make sense to have B and C's parent activity be A, but this means that each time we hit the "up" button from activities B or C, we land at activity A (and that's not always what is supposed to happen).

Does anybody have experience with this type of thing?

JasonWyatt
  • 5,275
  • 1
  • 31
  • 39

2 Answers2

60

from all three of your activities add the following

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }

    return(super.onOptionsItemSelected(item));
}

when you press the up button on your app it will invoke onOptionsItemSelected with the id of android.R.id.home just catch that case and manually call onBackPressed()

Eluvatar
  • 2,265
  • 19
  • 24
  • You may also consider creating a `BaseActivity` (which `ActivityA`, `ActivityB` and `ActivityC` would each extend), then you would only need to put the above method in your `BaseActivity` class. This will make for no code repetition and easier maintenance. – ban-geoengineering Feb 02 '16 at 15:08
  • This won't work incase of Deeplink. How you can go to parent activity in case of Deeplink? – sam_k Sep 27 '18 at 14:38
  • You're right, but that's because pressing back isn't designed to take you to the parent activity with Deeplink. Maybe you should look for some questions on going to the parent activity in android. – Eluvatar Nov 16 '18 at 00:27
  • Excellent answer! Is that means no new activities are created? – Daniel Carpio Contreras Dec 17 '18 at 03:20
0

KOTLIN

For anyone coming here in the age of Kotlin, if you set your toolbar up with the NavController, the "Up" button will behave like the back button.

toolbar.setupWithNavController(findNavController(R.id.nav_host_fragment),findViewById(R.id.full_drawer_layout))
Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61