I have just started playing around with Fragments and I am trying to dynamically add/replace fragments.
I can add them just fine, like so...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FragmentManager fragMan = getSupportFragmentManager();
FragmentTransaction fragTran = fragMan.beginTransaction();
FragOne fOne = new FragOne();
FragTwo fTwo = new FragTwo();
FragThree fThree = new FragThree();
fragTran.add(R.id.frag_cont_one, fOne);
fragTran.add(R.id.frag_cont_two, fTwo);
fragTran.commit();
}
But when i try to replace fTwo with fThree the application crashes. Heres how i am doing the replace
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();
Any ideas?
Thanks
10-08 21:30:44.710: E/Trace(3219): error opening trace file: No such file or directory (2)
10-08 21:30:44.869: D/AndroidRuntime(3219): Shutting down VM
10-08 21:30:44.869: W/dalvikvm(3219): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-08 21:30:44.889: E/AndroidRuntime(3219): FATAL EXCEPTION: main
10-08 21:30:44.889: E/AndroidRuntime(3219): java.lang.RuntimeException: Unable to start activity ComponentInfo{evans.louis.fragments/evans.louis.fragments.Home}: java.lang.IllegalStateException: commit already called
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.os.Looper.loop(Looper.java:137)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-08 21:30:44.889: E/AndroidRuntime(3219): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 21:30:44.889: E/AndroidRuntime(3219): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 21:30:44.889: E/AndroidRuntime(3219): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-08 21:30:44.889: E/AndroidRuntime(3219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-08 21:30:44.889: E/AndroidRuntime(3219): at dalvik.system.NativeStart.main(Native Method)
10-08 21:30:44.889: E/AndroidRuntime(3219): Caused by: java.lang.IllegalStateException: commit already called
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
10-08 21:30:44.889: E/AndroidRuntime(3219): at evans.louis.fragments.Home.onCreate(Home.java:37)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.Activity.performCreate(Activity.java:5104)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-08 21:30:44.889: E/AndroidRuntime(3219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-08 21:30:44.889: E/AndroidRuntime(3219): ... 11 more
EDIT: LogCat added
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.
The Fragment class is deprecated indeed, but the Support Library Fragments are here to stay and are part of the new Android Jetpack components. So yes, doing the "Adding Fragments to Activities" course in the Android Intermediate Track is very much still relevant, as fragments play a big part in Android development.
The system calls this method when Android needs the layout for the fragment. The Fragment class and Fragment Transaction class allow you to add, remove and replace fragments in the layout of your activity. Fragments can be dynamically modified by the transaction.
The problem is right there in your logcat output:
Caused by: java.lang.IllegalStateException: commit already called
You need to begin a new transaction:
fragTran = fragMan.beginTransaction();
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();
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