Moon Light Box

Time is Money

Android - ActionMode

| Comments

ActionMode is supported on library android.support.v7.

An action mode's lifecycle is as follows and we need override it.

  • onCreateActionMode(ActionMode, Menu) once on initial creation
  • onPrepareActionMode(ActionMode, Menu) after creation and any time the ActionMode is invalidated
  • onActionItemClicked(ActionMode, MenuItem) any time a contextual action button is clicked
  • onDestroyActionMode(ActionMode) when the action mode is closed
MainActivity.java
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            initMainFragment();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
    
    private void initMainFragment() {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment()).commit();
    }
}
MainFragment.java
public class MainFragment extends Fragment {
    private Activity mActivity;
    private ActionModeCallBack mActionModeCallBack;

    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState) {
        View rootView =
                inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
    
    @Override  
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mActivity.finish();
        mActionModeCallBack.stop();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActionMode();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void startActionMode() {
        mActionModeCallBack = new ActionModeCallBack(mActivity);
        mActionModeCallBack.start();
    }
}
ActionModeCallBack.java
public class ActionModeCallBack implements ActionMode.Callback {
    private Activity mActivity;
    
    public ActionModeCallBack(Activity activity) {
        mActivity = activity;
    }

    public void start() {
        mActivity.startActionMode(this);
    }
    
    public void stop() {
        mActivity.finish();
    }
    
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
}

Comments

comments powered by Disqus