2

I have attached navigation drawer to main Activity of my application. Navigation drawer have 4 options that will navigate user to 4 different fragments f1, f2, f3 and f4.

I have created a home page Fragment fh. I am not adding fh as a choices in Navigation Drawer.

Fragment fh populates data making http call.

Whenever user selects f1, f2, f3 or f4; the respective fragments need to be shown back.

Till here everything is working fine.

However, whenever user press back button, then fh should be displayed. ie. content_frame should contain fh. I am unable to do this portion.

Right now, whenever user press back button, the default view is blank.

Also, since fh makes a http call to populate data, so fh that was added to stack needs to be shown.

Here is my implementation:

MainActivity- OnCreate

mDrawerLayout.closeDrawer(mDrawerList);
setUpHomeFragment();

Here is how I am managing fragment, on navigation drawer item clicked...

private void showFrg(Fragment fragment){

   FragmentManager fragmentManager = getSupportFragmentManager();
   FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();
   fragmentTransaction.addToBackStack(null);
   fragmentTransaction.replace(R.id.content_frame, fragment).commit();
}

The setUpHomeFragment Function, that is called just once on activity oncreate()

private void setUpHomeFragment(){
        mLandingPageFragment=new ReadyMyOrderCollections();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, mLandingPageFragment);
        ft.addToBackStack(null);
        ft.commit();
    }

MainActivity XML Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/toolbar" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <include
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/list_view" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>
N J
  • 27,217
  • 13
  • 76
  • 96
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71

2 Answers2

2

I found the cleanest approach for this issue here:

https://stackoverflow.com/a/21093352/1348550

Call setUpLandingFragment just once on activity onCreate.

private void setUpLandingFragment(){
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.popBackStack();
        fragmentManager.beginTransaction()
                .add(R.id.content_frame, new LandingFragment()).commit();
    }

On navigation drawer item click:

private void changeFrag (Fragment f) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.popBackStack();
            fragmentManager.beginTransaction()
                    .add(R.id.content_frame, f).addToBackStack("fragBack").commit();
        }

The coolest part about this solution is, onBackPressed is not needed at all.

Community
  • 1
  • 1
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
1

My work around for this is

private int getFragmentCount() {
    return getSupportFragmentManager().getBackStackEntryCount();
}

In your Activity

@Override
public void onBackPressed() {
    if(getFragmentCount()>0) {
     getSupportFragmentManager().popBackStackImmediate();
    }else{
        super.onBackPressed();
    }
}
N J
  • 27,217
  • 13
  • 76
  • 96