我有一个Activity,它在框架布局中托管片段.如果我将显示的片段更改为另一个片段,则片段不会添加到后台,因此使用“后退”按钮会立即关闭应用而不是导航回来(OnBackPressed中的FragmentManager.BackStackEntryCount始终为0) ()-打回来).
在托管片段的“MainActivity”的viewmodel中,我通过Showviewmodel<> -method显示片段:
public class Mainviewmodel : Mvxviewmodel { public IMvxCommand ShowHomeCommand { get { return new MvxCommand(ShowHomeExecuted); } } private void ShowHomeExecuted() { Showviewmodel<Homeviewmodel>(); } }
fragment-class有一个注释,用于将viewmodel分配给host-activity:
[MvxFragment(typeof(Mainviewmodel),Resource.Id.fragment_container)] [Register("namespace.of.HomeFragment")]
我在Setup-class中使用默认的AndroidViewPresenter:
protected override IMvxAndroidViewPresenter CreateViewPresenter() { var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies); Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter); return mvxFragmentsPresenter; }
我期望在MvxFragment-Attribut或MvxFragment类中有一个参数“AddToBackstack”或类似的东西但是没有这样的东西.我是否遗漏了某些东西,或者目前在MvvmCross 4.0的新片段机制中是否支持后备栈?
解决方法
你可以做的是在你的MainActivity中添加这样的东西:
public override void OnBeforeFragmentChanging (IMvxCachedFragmentInfo fragmentInfo,Android.Support.V4.App.FragmentTransaction transaction) { var currentFrag = SupportFragmentManager.FindFragmentById (Resource.Id.content_frame) as MvxFragment; if(currentFrag != null && fragmentInfo.viewmodelType != typeof(Menuviewmodel) && currentFrag.FindAssociatedviewmodelType() != fragmentInfo.viewmodelType) fragmentInfo.AddToBackStack = true; base.OnBeforeFragmentChanging (fragmentInfo,transaction); }
这将在导航发生之前将片段添加到backstack.