ActionBar Back button opens drawer instead of going back

拈花ヽ惹草 提交于 2021-02-11 12:20:51

问题


In my app I have problem like this. Note that I'm working with fragments and I have drawer too.

That's the method in my MainActivity for drawer open/close.

    public void drawerInit() {

    toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer);
    view = findViewById(R.id.mainView);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            float moveFactor = (drawerView.getWidth() * slideOffset);
            view.setTranslationX(moveFactor);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };

    drawer.addDrawerListener(toggle);
    toggle.syncState();
}

Example I have 3 fragments (F1, F2, F3). F1 is my main fragment where I can open and close the drawer. When I'm opening F2 or F3 fragments, I need to change the drawer icon to back arrow. I'm doing this part successfully, but the problem is when I'm clicking on this back arrow, that opens the navigation drawer instead of going back. So how can I fix this part?

Here the part, where I'm changing the icon to back arrow in fragment.

((AppCompatActivity) getActivity()).getSupportActionBar().show();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);

回答1:


Add in your Activity

    public void crateMenuButton(){
        toggle.setDrawerIndicatorEnabled(true);
        if(toolbarDrawable == null) {
            toolbarDrawable = toolbar.getNavigationIcon();
        }
        toolbar.setNavigationIcon(toolbarDrawable);
        invalidateOptionsMenu();
        toggle.syncState();
    }

    public void createBackButton() {
        toggle.setDrawerIndicatorEnabled(false);
        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //if the drawerToggle is disabled, fall off to the home button action
                if (!toggle.isDrawerIndicatorEnabled()) {
                    // pop fragment here
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    if (fragmentManager.getBackStackEntryCount() > 0) {
                        fragmentManager.popBackStack();
                    }
                } else {
                    if (drawerLayout.isDrawerOpen(navigationView)) {
                        drawerLayout.closeDrawer(navigationView);
                    } else {
                        drawerLayout.openDrawer(navigationView);
                    }
                }
            }
        });
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white));
    }

Download Back Arrow

Then call from your fragment as your need

((YourActivity) getActivity()).createBackButton();
OR
((YourActivity) getActivity()).crateMenuButton();


来源:https://stackoverflow.com/questions/50156152/actionbar-back-button-opens-drawer-instead-of-going-back

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