Search code examples
javaandroidnavigationandroid-actionbarback

Handle Up navigation from Action Bar like Back navigation. How?


How can I handle a Up navigation like a Back navigation?

Is there a method for the Up navigation like for the Back navigation (onBackPressed())?

Or can I programm the finish() method, when I press the Up navigation?


Solution

  • Assuming by 'up navigation' you mean a tap on the app icon, you need to override onOptionsItemSelected() in your activity and handle it like so:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 'home' is the id for the icon click in the action bar (i.e. up/back).
        if (item.getItemId() == android.R.id.home) {
            // do your thing and return true
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }