I totally don't understand a piece of code in the Xamarin example about using Tab at here https://developer.xamarin.com/samples/HelloTabsICS
As I understand
FragmentManager.FindFragmentById
Fragment
Fragment
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
Fragment
fragmentContainer
FrameLayout
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
FrameLayout
Fragment
This is because that FrameLayout is containing a Fragment, that you can implement programmatically.
Let's assume you have a fragment, like FirstFrag.cs, containing no matter what UI elements doing no matter what code. You can instantiate this fragment inside your FrameLayout doing this :
FragmentTransaction tx = FragmentManager.BeginTransaction ();
tx.Replace (Resource.Id.fragmentContainer, new FirstFrag ());
tx.Commit(); //Where fragmentContainer is your FrameLayout.
//Also note that tx.Replace is acting like tx.Add if there is no fragment.
Then, using FindFragmentById on that FrameLayout, you will be able to get the FirstFrag contained in it as a Fragment object. Better, you can directly get it as a FirstFrag :
FirstFrag frag = FragmentManager.FindFragmentById<FirstFrag>(Resource.id.fragmentContainer);
The FrameLayout is simply acting as a container for your fragments. You can use any layout, depending on how you wish to display the fragment content.
Now let's assume you have a second fragment, called SecondFrag.cs. If you add it to your container using tx.Add(), it will stack with the first one into the container. If you use Replace instead, you will erase the first one. But it seems like a bad idea to have two fragments in the same container. Ideally, you want to have a single fragment per container.
If you really need to put two fragments inside a container, you can add a Tag to the fragment doing this :
FragmentTransaction tx = FragmentManager.BeginTransaction();
tx.Add(Resource.id.fragmentContainer, new SecondTag(), "YourTag");
tx.Commit();
Then, to find a precise fragment from your container, you can do this :
var frag = FragmentManager.FindFragmentByTag("YourTag");
But again, it's not a good idea. One fragment per container is the way to go.
NOTE : I had weird results trying to get the fragment from the container immediately after the commit. It was always giving me the previous content of the container. I guess it's planned to work this way, but it seems like the commit is really applied once the method it was called in is over.
Hope I've been clear enough, feel free to ask for more details !