I've got 3 simple fragments (for testing purposes)
Fragment1
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frameLHaupt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentHaupt">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Toolbar
android:id="@+id/toolbar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
</FrameLayout>
Fragment2:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentActionBar">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="149dp"
tools:layout_editor_absoluteY="174dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/nextFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next fragment" />
<Button
android:id="@+id/lastFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last fragment" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Bro welcome custom actionbar" />
</LinearLayout>
</RelativeLayout>
and Fragment3:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayoutViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentViewPager">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
Calling Fragment1 -> Fragment2:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, fragment2);
fragmentTransaction.commit();
=> This code works just fine, the fragment1 framelayout gets replaced by the framelayout of the fragment
But when I call Fragment2 -> Fragment3:
FragmentViewPager fragment2 = new FragmentViewPager();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLActionBar, fragment2);
fragmentTransaction.commit();
=> The Fragment3 wont get replaced but rather added on top of fragment2. W
I tried ussing getActivity().getSuportFragmentManager()
but the result remains the same, the fragment3 gets added on top of fragment2.
What can be the cause be? Am I missing something?
It seems like you're using different containers for all of your fragments. When you replace a fragment, the container ID represents the container the fragment lives in, not the ID of the view you're looking to replace. So using the new fragment root as the fragment container is placing each fragment inside the previous one.