Always returns to main fragment onBackpressed

901 views Asked by At

I have implemented Navigation Drawer in Activity. I have 3-4 fragments in my app.

Let's say there are 3 fragments : Fragment_1, Fragment_2, Fragment_3

When I start my app, Fragment_1 is loaded and i go to another fragments using navigation drawer.

So it's like

Fragment_1 -> Fragment_2 -> Fragment_3 -> Fragment_1 -> Fragment_3

Now, when I press back button it's like

Fragment_3 -> Fragment_1 -> Fragment_3 -> Fragment_2 -> Fragment_1

But, I want something like if wherever i am when i press back key, i navigate back to Fragment_1 always.

I am using below code to navigate to another fragment :-

nav_menu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (position) {
            case 0:
                DashboardFragment dashboardFragment = new DashboardFragment();
                fragmentTransaction.replace(R.id.mainContainer, dashboardFragment, dashboardFragment.getClass().getName());
                break;

            case 1:
                ProfileFragment profileFragment = new ProfileFragment();
                fragmentTransaction.replace(R.id.mainContainer, profileFragment, profileFragment.getClass().getName());
                fragmentTransaction.addToBackStack(null);
                break;

            case 2:
                ContactUsFragment contactUsFragment = new ContactUsFragment();
                fragmentTransaction.replace(R.id.mainContainer, contactUsFragment, contactUsFragment.getClass().getName());
                fragmentTransaction.addToBackStack(null);
                break;

            case 3:
                AboutUsFragment aboutUsFragment = new AboutUsFragment();
                fragmentTransaction.replace(R.id.mainContainer, aboutUsFragment, aboutUsFragment.getClass().getName());
                fragmentTransaction.addToBackStack(null);
                break;

            default:
                break;
        }
        fragmentTransaction.commit();
        drawer.closeDrawer(GravityCompat.START);
    }
});

Please help...

2

There are 2 answers

5
Rishabh Mahatha On
@Override
    public void onBackPressed() {

        if(!(getSupportFragmentManager().findFragmentById(R.id.mainContainer) instanceof DashboardFragment))
        {
            DashboardFragment dashboardFragment = new DashboardFragment();
            fragmentTransaction.replace(R.id.mainContainer, dashboardFragment, dashboardFragment.getClass().getName());
        }else {
            super.onBackPressed();
        }
    }

Use this method into main activity will solve your problem.

0
Fernando Romo Vázquez On

I've changed next lines in your code and it worked just fine

Replace:

fragmentTransaction.replace(R.id.mainContainer, dashboardFragment, 
dashboardFragment.getClass().getName());

With:

getSupportFragmentManager().beginTransaction().replace(R.id.mainContainer,
dashboardFragment).commit();