Android : Hiding action bar in fragment

自作多情 提交于 2019-12-12 01:28:34

问题


I have 2 Fragments mainFragment and childFragment. I want to show actionbar in mainFragment but wants to hide in childFragment. All is working fine except that when I comes back to mainFragment from childFragment and again goes to childFragment action bar shows for seconds before get hidden ..

I don't know why ? HELP

In childFragment I am doing this

@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;
        }
    });
}

回答1:


You should handle all transitions in your main activity... There you should handle all the logic according to your needs - such as hiding/showing the action bar and such...




回答2:


Try to do this in your fragmen's onActivityCreated method:

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

Edit: I assume that you are handling your fragment transactions in your activity. Before you replace your fragment in activity with:

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

do your actionbar hide and show operations according to which fragment you are replacing. (main or child)



来源:https://stackoverflow.com/questions/31565364/android-hiding-action-bar-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!