How to handle Back Button to remove current fragment In MvvmCross

試著忘記壹切 提交于 2019-12-11 03:28:59

问题


My MainMenu is the first launched activity or page which will show the MainPage as the Fragment

in this MainMenu,

a) it has a Left Drawer. When user click on an item in the drawer, it will be displayed as fragment.

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

The Problem:

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();
}

回答1:


You can add the fragment to the backstack of the fragment manager with this:

var ft = SupportFragmentManager.BeginTransaction();
ft.AddToBackStack(fragInfo.Tag);
ft.Commit();

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




回答2:


To add fragment to backstack it is possible to specify AddToBackStack = true in MvxFragmentPresentation attribute:

[MvxFragmentPresentation(typeof(YourViewModel), Resource.Id.your_content_frame, AddToBackStack = true)]


来源:https://stackoverflow.com/questions/32286428/how-to-handle-back-button-to-remove-current-fragment-in-mvvmcross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!