Up Navigation (Action Bar's back arrow) is not working for fragments

℡╲_俬逩灬. 提交于 2019-11-30 19:14:30

Finally got the answer. In my scenario I'm disabling the drawer indicator by mActionBarDrawerToggle.setDrawerIndicatorEnabled(false); and due to this Navigation icon clicks got disabled. To enable this I've to add ToolbarNavigationClickListener to ActionBarDrawerToggle which will enable navigation icon clicks.

Below is the my working code :-

mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

Refer this link for more clarification

After struggling with the same problem for ages, I eventually managed to get the Up button to work in fragments with this code. You must have setHasOptionsMenu set up in onCreate() or onCreateView()

setHasOptionsMenu(true);

Then, in onOptionsItemSelected() add this check for the up / home button being pressed to your switch() [or if...] statement:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    x.L();

    switch (item.getItemId()) {
        case android.R.id.home :
            getActivity().onBackPressed();

            break;

        case R.id.mn_exit :
            exitFragment();

            break;

        default :
            break;
    }

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