Question: Question:
I'm currently trying to switch screens using Fragment
, but when I do transaction.replace
, the screen of layout_A and the screen of layout_B are displayed in an overlapping state. Even if I tried transaction.remove
and transaction.add
, the result was the same. Is Fragment
used incorrectly? Where can I fix it to switch screens? I'm sorry, but thank you.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_changeMode, new FragmentB());
transaction.addToBackStack(null);
transaction.commit();
layout_A.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="taro.ghost.fragment.FragmentA"/>
</RelativeLayout>
layout_B.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="@drawable/contact" />
</RelativeLayout>
Answer: Answer:
The Fragment
specified in the layout XML must not be add
or replace
.
In this case, layout_A.xml
as follows so that FragmentA
is added around onCreate()
.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</RelativeLayout>