如何处理“后退"按钮以删除MvvmCross中的当前片段 [英] How to handle Back Button to remove current fragment In MvvmCross
问题描述
我的MainMenu是第一个启动的活动或页面,它将把MainPage显示为片段
My MainMenu is the first launched activity or page which will show the MainPage as the Fragment
在此MainMenu中,
in this MainMenu,
a)它有一个左抽屉.当用户单击抽屉中的项目时,该项目将显示为片段.
a) it has a Left Drawer. When user click on an item in the drawer, it will be displayed as fragment.
在此主页中,
b)它在操作栏中具有下拉菜单.当用户单击菜单中的项目时
它会显示为片段
In this MainPage,
b) it has dropdown menu in Action-Bar. when user click an item in the menu
it will be displayed as fragment
问题:
1)用户单击MainPage(b):Action_bar下拉菜单中的一项,如何删除或不显示此当前显示片段,当用户单击后退"按钮并显示主页内容时
1) User click an item in MainPage(b) :Action_bar dropdown menu, how to remove or not to show this current display fragment WHEN user click the back button and show the main page content
我尝试了下面的代码,它不起作用
I tried below code, it does not work
public override void OnBackPressed()
{
if (DrawerLayout != null && DrawerLayout.IsDrawerOpen(GravityCompat.Start))
DrawerLayout.CloseDrawers();
else
base.OnBackPressed();
}
推荐答案
您可以使用以下方法将片段添加到片段管理器的后台:
You can add the fragment to the backstack of the fragment manager with this:
var ft = SupportFragmentManager.BeginTransaction();
ft.AddToBackStack(fragInfo.Tag);
ft.Commit();
我建议您研究一下MvvmCross的Android示例.默认情况下,它处理很多事情,例如后堆栈. https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples
I would recommend looking into the Android sample of MvvmCross. It handles a lot of things by default, like the backstack. https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples
这篇关于如何处理“后退"按钮以删除MvvmCross中的当前片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!