Reputation: 9915
I have a layout of
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="wrap_content">
<LinearLayout android:id="@+id/linearLayout13" android:layout_height="wrap_content" android:paddingRight="70dp" android:orientation="vertical" android:paddingLeft="70dp" android:layout_width="wrap_content">
<ImageView android:id="@+id/baby_icon" android:layout_height="wrap_content" android:src="@drawable/baby" android:clickable="true" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</LinearLayout>
<fragment
android:name="com.nicu.health.FragAFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yellow_cardlist_fragment"
android:layout_weight="1"
>
</fragment>
</LinearLayout>
On the startup this does show the correct fragment(FragAFragment). Now on the button click for baby_icon
I try to remove the current fragment and add a new one(FragBFragment) which has an entirely different layout.
Though I see that the onCreateView
method is called and it does return a non-null view but the UI on the screen of the new fragment does not get updated. I use the below code to update the fragment.
Fragment baby = FragBFragment.newInstance(1);
FragmentTransaction ft = getFragmentManager().beginTransaction();
// ft.remove(getFragmentManager().findFragmentById(R.id.yellow_cardlist_fragment));
// ft.add(R.id.yellow_cardlist_fragment, baby);
ft.replace(R.id.yellow_cardlist_fragment, baby);
ft.addToBackStack(null);
Log.i(TAG, "Code for commit = "+ft.commit());
I had tried all combinations of remove, replace and add to get the fragment thing workng, but in vain!
Also further I tried with the code as
<fragment
android:name="com.nicu.health.FragBFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yellow_cardlist_fragment"
android:layout_weight="1"
>
and this does work to show the second fragment on startup!!!!
Help will be reeally appreicated.
Thanks,
Upvotes: 3
Views: 6097
Reputation: 11267
Ok, my first guess: You forgot to say ft.commit();
My second guess:
Lets say u have Activity AB which controls Fragment A and Fragment B.
You also have 3 layouts. Layout_ActivityAB, Layout_FragA, Layout_FragB.
So setContentView(R.layout.Layout_ActivityAB) is called inside Activity AB. That will be the layout in which u tell android where to place the fragment in the screen.
Inside Fragment A or Fragment B:
@Override
//onCreateView is called when an instance of this class is first created.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.Layout_FragA, container, false); return rootView;
}
R.layout.Layout_FragA is called inside the Fragment. Layout_FragA is the actual content of the Fragment.
Upvotes: 0
Reputation: 9915
Answer to my question is
Android: can't replace one fragment with another
I added dynamic fragments on start of the main activity and onclick I did a ft.replace to replace the old fragment!
Upvotes: 7