I need to replace a Fragment
in one Activity
with another Fragment
, below is the layer file of the Activity
:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/home_layout_container"> <fragment android:name="com.foo.FragA" android:id="@+id/home_list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout>
so by default there is FragA, now I want to replace it with FragB in activity, I did:
public void onRegionClicked(Region region) { RegionInfoFragment rif = RegionInfoFragment.newInstance(region); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.home_list, rif); }
But I got exception:
>6:24:40.685: ERROR/AndroidRuntime(9194): Uncaught handler: thread main exiting due to uncaught exception 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addViewInner(ViewGroup.java:1857) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1752) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1709) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.view.ViewGroup.addView(ViewGroup.java:1689) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:578) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1219) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:380) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Handler.handleCallback(Handler.java:587) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Handler.dispatchMessage(Handler.java:92) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.os.Looper.loop(Looper.java:123) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at android.app.ActivityThread.main(ActivityThread.java:4363) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at java.lang.reflect.Method.invokeNative(Native Method) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at java.lang.reflect.Method.invoke(Method.java:521) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 05-06 16:24:40.692: ERROR/AndroidRuntime(9194): at dalvik.system.NativeStart.main(Native Method)
What can I do?
Thanks!
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken fragments to pass the data between two fragments.
Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With