如何使用制表符和片段,并且不会丢失有关片段更改/替换的参数
本文关键字:片段 替换 参数 制表符 何使用 | 更新日期: 2023-09-27 18:34:40
我以这种方式使用带有片段的选项卡(一个活动有一个片段,在单击选项卡时被另一个片段替换(:
public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
switch (tab.Position)
{
case 0:
//Show content of tab1
FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction ();
fragmentTx.SetTransition (FragmentTransit.FragmentFade);
Music_SelectTheme aDifferentDetailsFrag_3 = new Music_SelectTheme ();
fragmentTx.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag_3);
fragmentTx.AddToBackStack(null);
fragmentTx.Commit();
break;
case 1:
//Show content of tab2
FragmentTransaction fragmentTx2 = this.FragmentManager.BeginTransaction();
fragmentTx2.SetTransition(FragmentTransit.FragmentFade);
Audio aDifferentDetailsFrag2 = new Audio();
fragmentTx2.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag2);
fragmentTx2.AddToBackStack(null);
fragmentTx2.Commit();
break;
case 2:
//Show content of tab3
FragmentTransaction fragmentTx3 = this.FragmentManager.BeginTransaction();
fragmentTx3.SetTransition(FragmentTransit.FragmentFade);
SoundEffects aDifferentDetailsFrag3 = new SoundEffects();
fragmentTx3.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag3);
fragmentTx3.AddToBackStack(null);
fragmentTx3.Commit();
break;
case 3:
//Show content of tab3
FragmentTransaction fragmentTx4 = this.FragmentManager.BeginTransaction();
fragmentTx4.SetTransition(FragmentTransit.FragmentFade);
Dices aDifferentDetailsFrag4 = new Dices();
fragmentTx4.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag4);
fragmentTx4.AddToBackStack(null);
fragmentTx4.Commit();
break;
default:
break;
}
}
有时,一个片段会在内部替换另一个片段,而无需单击选项卡,如下所示:
FragmentTransaction fragmentTx2 = this.FragmentManager.BeginTransaction ();
fragmentTx2.SetTransition (FragmentTransit.FragmentFade);
Audio aDifferentDetailsFrag2 = new Audio {Arguments = myBundle};
fragmentTx2.Replace (Resource.Id.fragment_container, aDifferentDetailsFrag2);
fragmentTx2.AddToBackStack (null);
fragmentTx2.Commit ();
如您所见,我将参数放在片段中,但是如果我单击另一个选项卡,然后再次返回此选项卡,则参数为空。为什么会这样?我必须再次从数据库中提取数据,或者我可以以某种方式避免它?
将我需要的所有内容保存在活动中并使用这样的函数从那里检索它是一个坏主意吗?
var list = Activity.GetList((;
我已经找到了最好的解决方案,
我不必过多地使用参数。使用 Application 类全局定义全局变量要好得多。