Android:隐藏片段中的操作栏

时间:2015-07-22 14:00:24

标签: android android-fragments android-actionbar fragment

我有2个片段mainFragmentchildFragment。我想在mainFragment中显示操作栏,但希望隐藏在childFragment中。一切正常,但是当我从mainFragment回到childFragment并再次进入childFragment动作栏显示几秒钟之后才被隐藏...

我不知道为什么? 帮助

childFragment我正在做这个

@Override
public void onResume() {
    super.onResume();
    ((ActionBarActivity) getActivity()).getSupportActionBar().hide();
    mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                // handle back button's click listener

                mainActivity.onBackPressed();
                ((ActionBarActivity) getActivity()).getSupportActionBar().show();
                mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                return true;
            }
            return false;
        }
    });
}

2 个答案:

答案 0 :(得分:0)

您应该处理主要活动中的所有转换......您应该根据需要处理所有逻辑 - 例如隐藏/显示操作栏等...

答案 1 :(得分:0)

尝试在你的framen onActivityCreated方法中执行此操作:

((ActionBarActivity) getActivity()).getSupportActionBar().hide();
mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

编辑: 我假设您正在处理活动中的片段事务。在使用以下内容替换活动中的片段之前:

getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new YourFragment()).commit();

根据要替换的片段隐藏操作栏并显示操作。 (主要或儿童)