Xamarin
用于使用 .NET 和 C# 构建 Android 和 iOS 应用的 Microsoft 开源应用平台。
15 个问题
我创建了一个活动和 2 个片段。片段 B 位于片段 A 内。当我单击手机上的后退按钮时,它不会从片段 B 转到片段 A。但它会使应用程序崩溃。我正在维护一个堆栈。
public void ShowFragment(SupportFragment fragment)
{
if (fragment.IsVisible)
{
return;
}
Android.Support.V4.App.FragmentTransaction trans = SupportFragmentManager.BeginTransaction();
fragment.View.BringToFront();
trans.Hide(mCurrentFragment);
Console.WriteLine("Hiding fragemnt current " + mCurrentFragment.Tag+" fragment "+fragment.Tag);
trans.Show(fragment);
if (mStackFragments.Count != 0)
{
SupportFragment sf = mStackFragments.Pop();
if (fragment != sf)
{
mStackFragments.Push(sf);
mStackFragments.Push(fragment);
}
else
{
mStackFragments.Push(sf);
}
}
else
{
mStackFragments.Push(fragment);
}
mCurrentFragment = fragment;
trans.Commit();
LoadFragmentTitleTxtView();
LoadActionBarSpinner();
}
上面的方法用于显示片段/主屏幕,而以下代码用于 OnBackPressed。
public override void OnBackPressed()
{
if (mDrawerLayout.IsDrawerOpen((int)GravityFlags.Left))
{
mDrawerLayout.CloseDrawer((int)GravityFlags.Left);
}
else
{
if (mStackFragments.Count > 1)
{
//mStackFragments.Pop();
Console.WriteLine("Popping 1st " + mStackFragments.Pop().Tag+" Count "+mStackFragments.Count);
SupportFragment frag = mStackFragments.Pop();
Console.WriteLine("Poppoing 2nd " + frag.Tag+" Count "+mStackFragments.Count);
if (frag == chaptersFragment)
{
if (chapterfiltertext != "")
{
item.ExpandActionView();
sssearchview.SetQuery(chapterfiltertext, false);
mainview.Animate().Alpha(1f).SetListener(null);
}
}
Console.WriteLine("In back method.. "+frag.Tag);
ShowFragment(frag);
}
else
{
Finish();
}
}
}
它适用于其中没有任何嵌套片段的片段。
这是嵌套片段 B 从片段 A 加载到主屏幕上的代码。
FragmentTransaction tx = context.SupportFragmentManager.BeginTransaction();
FragmentB fragmentB = new FragmentB();
Bundle args = new Bundle();
args.PutString("letter", tmp1);
fragmentB.Arguments = args;
tx.Replace(Resource.Id.main, fragmentB, "innerDictFrag");
tx.AddToBackStack(null);
context.mStackFragments.Push(fragmentB);
tx.Commit();
Note:此问题总结整理于: Nested fragments are not working on back click in xamarin android
欢迎来到我们的 Microsoft 问答平台!
如果要将 fragment1 传输到 fragment2,请使用以下代码,不要更改代码顺序。例如,当前是 fragment1,然后我导航到 fragment2
public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view= inflater.Inflate(Resource.Layout.layout1, container, false);
Button button1 = view.FindViewById<Button>(Resource.Id.button1);
button1.Click += Button1_Click;
return view;
}
private void Button1_Click(object sender, EventArgs e)
{
FragmentTransaction transaction = this.FragmentManager.BeginTransaction();
Fragment2 fragment = new Fragment2();
transaction.AddToBackStack(null);
transaction.Replace(Resource.Id.FramePage, fragment);
transaction.Commit();
}
}
在 fragment2 中,可以单击后退按钮以后退 fragment1,或单击按钮执行this.FragmentManager.PopBackStack();
public class Fragment2 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view= inflater.Inflate(Resource.Layout.layout2, container, false);
Button popBtn = view.FindViewById<Button>(Resource.Id.popBtn);
popBtn.Click += PopBtn_Click;
return view;
}
private void PopBtn_Click(object sender, EventArgs e)
{
this.FragmentManager.PopBackStack();
}
}
如果回复有帮助,请点击“接受答案”并点赞。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。