Different functionality to back button and toolbar close button

113 Views Asked by At

In my android app I am stacking different instances of the same activity over top of each other (it is a requirement and I cannot change that).

I also have a close button on my activity's toolbar where the current functionality is that if user clicks on the close button it will close the current instance of the activity. Back button also have the same functionality.

But I want to change this behaviour. The back button should work the same way it is working now (finish the current instance) but the close button should finish all the instances of that activity and return to the parent activity where the transition started.

I cannot use the intent flags like Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOPbecause then the back button will also close all the instances of the activity.

I need something on theonOptionsItemSelected which close the activity and return to the parent activity. Please provide any hint if you can. Thanks

1

There are 1 best solutions below

2
On

You can use different action on both clicks

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case YOUR_ID_IN_MENU:
                onCloseClick();
                break;
        }

        return super.onOptionsItemSelected(item);
    }