45

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!

N J
  • 25,967
  • 13
  • 73
  • 94
hzxu
  • 4,295
  • 7
  • 47
  • 82

3 Answers3

119

You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Thanks, I'll try to add them programmatically. – hzxu May 06 '11 at 07:37
  • Can anyone provide a link showing the proper way to do this? Thanks. – James Oct 07 '11 at 04:12
  • 6
    @James - Take a look at the [Fragment Android Documentation](http://developer.android.com/guide/topics/fundamentals/fragments.html). Look at the code snippet under, **"Or, programmatically add the fragment to an existing ViewGroup."** – Peter Ajtai Nov 03 '11 at 15:31
  • use this http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace – Rishabh Agrawal Nov 05 '12 at 06:30
  • 3
    Why do the dynamically added fragments overlap some time?? – Snehal Poyrekar Mar 15 '13 at 12:20
  • @NightCrawler getSupportFragmentManager and getChildFragmentManager have a slight difference described [here](http://stackoverflow.com/a/14775322/2468026) and using them together can create an overlapping fragment. – Adam Wigren Jan 28 '15 at 09:25
  • 1
    This comment held true. Change the fragment creation away from static xml declaration and use the FragmentTransaction to add it. Also, when replacing the fragment using FragmentTransaction be sure to use the container holding the fragment as the first param to replace() instead of passing in the id of the actual fragment. See section "Performing Fragment Transactions" in the link provided by @PeterAjtai – Bamerza Aug 12 '16 at 16:39
  • this article provides the solution : http://wptrafficanalyzer.in/blog/dynamically-add-fragments-to-an-activity-in-android/ – Hossein Aug 19 '17 at 15:45
5

Try to commit() at the end of the ft, you need to commit() your changes to let it know.

GôTô
  • 7,771
  • 3
  • 30
  • 42
sumakira
  • 67
  • 1
  • 1
1
getFragmentManger()
  .beginTransaction
  .replace(R.id.frame,Yourfragment.newInstance(),null)
  .addtobackstack
  .commit();

People always say I am one-liner, so here is one-liner solution for you

Jemshit Iskenderov
  • 7,417
  • 5
  • 48
  • 91
raj kavadia
  • 847
  • 1
  • 6
  • 20