方向更改后恢复ActionMode

时间:2012-08-15 05:47:58

标签: android android-actionbar actionbarsherlock

我有listfragment,它启动了actionmode。我正在使用actionbarsherlock。但是当方向改变时,或者当我开始新的活动上下文动作栏消失时。 有没有办法恢复它,而不是再次启动它?在google gmail应用程序中,无论是旋转手机还是转到详细信息屏幕,它都会保留在屏幕上。

1 个答案:

答案 0 :(得分:17)

更好的方法是使用onSaveInstanceState在旋转之前保存ActionMode状态。

public abstract class MyActivity extends SherlockFragmentActivity{

    private boolean isInActionMode = false;

    @Override
    public void onCreate(Bundle state){
         super.onCreate(state);

         if (state != null && state.getBoolean("ActionMode", false)){
                startActionMode(new MyActionMode());
         }else{
             ///whatever you'd normally do
         }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
         // Save isInActionMode value
         outState.putBoolean("ActionMode", isInActionMode);

         super.onSaveInstanceState(outState);
    }

    public void onWhateverEventNormallyStartsYourActionMode(){
        startActionMode(new MyActionMode());
    }

    public class MyActionMode implements ActionMode.Callback{

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            isInActionMode = true;
            ///whatever you'd normally do
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            ///whatever you'd normally do
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            ///whatever you'd normally do
        }

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

根据Saran的评论更新。