我正在尝试为以下情况创建一个简洁的解决方案:
我已经构建了一个应用程序,需要凭据来验证用户。每当API想要重新验证用户时,我希望强制用户返回到登录视图。这个功能非常好用,但是当用户在设备上按下后退按钮时,之前的视图会被显示。我希望当用户按下后退按钮时,应用程序退出。
MvvmCross有使用MvxPresentationHint的选项。我创建了一个CustomAndroidViewPresenter,并创建了以下MvxAndroidSetup:
我已经构建了一个应用程序,需要凭据来验证用户。每当API想要重新验证用户时,我希望强制用户返回到登录视图。这个功能非常好用,但是当用户在设备上按下后退按钮时,之前的视图会被显示。我希望当用户按下后退按钮时,应用程序退出。
MvvmCross有使用MvxPresentationHint的选项。我创建了一个CustomAndroidViewPresenter,并创建了以下MvxAndroidSetup:
public class Setup : MvxAndroidSetup
{
private CustomAndroidViewPresenter _presenter;
public Setup(Context applicationContext)
: base(applicationContext)
{
_presenter = new CustomAndroidViewPresenter(applicationContext);
}
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
Mvx.RegisterSingleton(_presenter);
return _presenter;
}
}
我知道你应该在活动上调用Finish(); 以防止后退按钮导航回未经身份验证的视图。但是将applicationContext转换为会抛出异常。
public CustomAndroidViewPresenter(Context context)
{
_context = context;
}
public override void ChangePresentation(MvxPresentationHint hint)
{
if (hint is LoginOnlyBackStackHint)
{
var context = Application.Context;
if (context is Activity)
{
// Context is NOT activity
}
try
{
Activity x = (Activity) context;
x.Finish();
// Exception is thrown
}
catch (Exception ex)
{
}
}
base.ChangePresentation(hint);
}
有人有实现这个的想法吗?
非常感谢。