简体   繁体   中英

How to go to root activity from any fragment by pressing back button?

i have an activity with a navigation drawer built using the library https://github.com/mikepenz/MaterialDrawer

I want to load 4 different pages from selecting 4 different items from the navigation drawer.

I decided to use fragments for each item in the navigation drawer, so when any item is pressed, a new fragment for that option is created and shown to the user.

What i want is that, regardless of which fragment the user is currently in, or which fragment he came from, whenever he presses the back button, he is returned to the original activity.

This is the code i have so far, but apparently the Fragment Backstack is not being updated.

result = new DrawerBuilder().withSelectedItem(-1)
                .withActivity(this)
                .withToolbar(toolbar)
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        item1,item2,item3,item4
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        result.setSelection(position,false);
                        clearStack();
                        switch (position) {
                            case 1:
                                fragment = new admin_fragment_data_entry();
                                break;
                            case 2:
                                fragment = new admin_fragment_data_edit();
                                break;
                            case 3:
                                break;
                            case 4:
                                break;
                        }
                        initiateFragment();
                        return false;
                    }
                }
                )
                .build();


    @Override
    public void onBackPressed() {

        int count = getFragmentManager().getBackStackEntryCount();

        //handle the back press :D close the drawer first and if the drawer is closed close the activity
        if (result != null && result.isDrawerOpen()) {
            result.closeDrawer();
        }
        else if (count == 0) {
            super.onBackPressed();
            //additional code
        }
        else {
            getFragmentManager().popBackStack();
        }


    }

    private void initiateFragment(){
        if (fragment != null) {


            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.container_body, fragment);
            ft.commit();
        }
    }

    private void clearStack(){
        int count = getFragmentManager().getBackStackEntryCount();
        while(count > 0){
            getFragmentManager().popBackStack();
            count--;
        }
    }

Any help would be highly appreciated.

Also, should i have used fragments here? Or i should have just started 4 different activities for 4 different options from navigation drawer?

Are you using the correct fragment manager to get your backstack count?

I notice in other parts of your class you're using getSupportFragmentManager instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM