how to know when back arrow is pressed in actionmode

Viewed 284

i have an actionmode bar that has an arrow button and a delete icon. see pic belowenter image description here

i am trying to identify in my code when the arraow button is pressed but not able to. i have the following code

   protected ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.item_selected, menu);
            context_menu = menu;
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    Log.d("BACKPRESS", "BACKPRESSD");
                    return true;
                case R.id.menu_action_delete:

                    return true;
                default:
                    return false;
            }
        }


        @Override
        public void onDestroyActionMode(ActionMode mode) {
           mActionMode = null;

        }
    };

when i press the back arrow button, it does not execute the case statement in onActionItemClicked function

case android.R.id.home:
     Log.d("BACKPRESS", "BACKPRESSD");
    return true;

but instead execute onDestroyActionMode fucntion. i would like to know the following:

  1. how can i write code to determine when back arrow is pressed? for example, i want something like if(backarrow is pressed on the actionmode bar) do something

  2. if you notice, by default the actionmode bar is black with a red shadow line. how can i change the color of the actionmode bar and the red line?

  3. related to question 2, can i have different activities with different color for the action mode bar or im limited to one color for all my activities? if yes, how?

1 Answers

try this code ->

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:
            Intent homeIntent = new Intent(this, HomeActivity.class);
            startActivity(homeIntent);
    }
    return (super.onOptionsItemSelected(menuItem));
}
Related