I've made an app with multiple tabs. Using code like this:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var tabs = Enum.GetValues(typeof(Tab));
foreach (Tab tab in tabs)
{
AddTab(tab, (Fragment) Activator.CreateInstance(Type.GetType("MyNamespace." + tab.ToString() + "Fragment")));
}
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView(Resource.Layout.MainTabActivityLayout);
}
private void AddTab(Tab tab, Fragment fragment)
{
Android.App.ActionBar.Tab droidTab = this.ActionBar.NewTab();
droidTab.SetCustomView(new TabLayout(this));
droidTab.SetTag(tab.ToString());
droidTab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
{
e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment);
};
this.ActionBar.AddTab(droidTab);
}
This works fine, I can switch between tabs and I see the different fragment views.
On the first tab fragment, I'd like to click a button and show a different fragment. Without showing the whole fragment code, this is the part of the fragment that is important:
protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
FragmentTransaction fragmentTransaction = FragmentManager.BeginTransaction();
childFragment = new MyNamespace.MainTabActivity.SampleTabFragment();
fragmentTransaction.Replace(Resource.Id.fragmentContainer, childFragment);
fragmentTransaction.AddToBackStack(null);
fragmentTransaction.Commit();
}
This works too. The two problems I'm running into, but I think are related:
The user is viewing the sub-fragment and decides to click on a different tab, but then comes back to the first tab, where the child fragment was being shown. My expected/wanted behavior would be that the child fragment is shown. The actual behavior is that the parent fragment is shown again.
The user is viewing the sub-fragment and decides to change the orientation of the device. Once again, I'd expect the child fragment to be shown, but the parent fragment is shown again instead.
Can anyone guide me on how to do what I want to do?