Hung Nguyen
Hung Nguyen

Reputation: 43

Which Fragment will be replaced when use fragmentTransaction.replace?

I have a activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

 <fragment
android:name="fragments"
android:id="@+id/lm_fragment"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"/>

 <fragment
android:name="fragments"
android:id="@+id/pm_fragment"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="2"/>

</LinearLayout>

So I have 2 Fragment in my Activity And when I add a Fragment in android.R.id.content, which Fragment will be added? My Mainactivity.java:

if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
 LM_Fragment lm_Fragment = new LM_Fragment();
 fragmentTransaction.replace(android.R.id.content, lm_Fragment);
 }else{
 PM_Fragment pm_Fragment = new PM_Fragment();
 fragmentTransaction.replace(android.R.id.content, pm_Fragment);
 }

 fragmentTransaction.commit();
 }

LM_Fragment and PM_Fragment is used for Landscape mode and Portrait mode. And when I use fragmentTransaction.replace(android.R.id.content, lm_Fragment); It will be added in 1 in 2 fragment which I specified in xml, but where lm_fragment or pm_fragment? Thank you!

Upvotes: 0

Views: 92

Answers (1)

Sviatoslav Melnychenko
Sviatoslav Melnychenko

Reputation: 543

Here is replace method signature

public abstract FragmentTransaction replace (int containerViewId, Fragment fragment)

You don't have such id android.R.id.content in your layout. Thing is that you're using static fragments. But you need dynamic. Replace both fragment tag with single FrameLayout with id=android.R.id.content. Or better use your own id. E.g. "fragment_container", and then call

fragmentTransaction.replace(R.id.fragment_container, lm_Fragment);

and

fragmentTransaction.replace(R.id.fragment_container, pm_Fragment);

Upvotes: 1

Related Questions