0

I want to change the destination on the up arrow created from the following actionbar:

    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();

    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);

I realize that some aspects like the icon are modifiable. What about the functionality and changing R.id.home to something else? Is this possible, or is the up arrow off limits, and I will have to create one myself and then modify it in my *.xml layout?

My end goal is to have it to send me to the previous activity instead of to the home affordance.

I already have an onBackPressed as:

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}//end onBackPressed()
T.Woody
  • 1,142
  • 2
  • 11
  • 25
  • 1
    Possible duplicate of [How to change HomeAsUp indicator in new AppCompat Toolbar?](https://stackoverflow.com/questions/26778278/how-to-change-homeasup-indicator-in-new-appcompat-toolbar) – Yash Sep 07 '18 at 03:32
  • @stupidly_logical that question is referencing the icons while mine is asking about the backend. – T.Woody Sep 07 '18 at 03:36

1 Answers1

0

Try this to change the destination:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getId() == android.R.id.home) {
        //To go to the previous activity in the stack
        super.onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

super.onBackPressed() calls finish() by default if you haven't overridden onBackPressed().

Yash
  • 3,438
  • 2
  • 17
  • 33
  • In your previous activity, did you call finish()? – Yash Sep 07 '18 at 03:46
  • I meant, if you called finish() in your previous activity when you started the new activity, it will remove the previous activity from the stack so you won't be able to access it by onBackPressed() – Yash Sep 07 '18 at 03:49
  • Yes it helps. So as I was saying that if you called finish() in bar when you started baz, it will remove bar from the activity stack and so, when you'll call onBackPressed() in baz, you'll be taken to foo and not bar. – Yash Sep 07 '18 at 03:56
  • I did some testing, and this method is far from necessary. The reason why the up arrow was sending me to my root activity is because I defined a parent in my manifest. – T.Woody Sep 07 '18 at 23:38