In this tutorial, you will learn how to replace fragments in the FrameLayout layout in Android.
You should always add, remove, and replace your fragments problematically. As such I suggest you replace your “TitleFragment” fragments with containers such as FrameLayout. I had to use android.support.v4.app.Fragment; instead of android.app.Fragment;. This is the code I got it to work with:
1 2 3 4 5 6 7 8 9 |
<FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/view" android:layout_marginTop="@dimen/medium_margin"> </FrameLayout> |
setFragment method called from displayView method,
1 2 3 4 5 6 7 |
protected void setFragment(Fragment fragment) { android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); trans.replace(R.id.fragment_container, fragment); trans.commit(); } |
And to set it (from the navbar onNavigationItemSelected(), you do this:
1 2 3 |
setFragment(new TitleFragment()); |
Leave a Comment