Xamarin在onSavedInstanceState之后无法执行此操作

时间:2017-09-26 15:43:21

标签: c# android xamarin.android onbackpressed

我正在打开Zxing扫描仪的相机屏幕,我也使用自定义模板。在我的自定义模板中,我设置了SetDisplayHomeAsUpEnabled(true)但是当我点击它时,我收到此错误Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState

这是我的点击事件:

private void Toolbar_NavigationClick(object sender, Android.Support.V7.Widget.Toolbar.NavigationClickEventArgs e)
    {
        var mainActivity = Activity as MainActivity;
        mainActivity?.OnBackPressed();
    }

我的错误的完整堆栈跟踪:

[ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
--- End of managed Java.Lang.IllegalStateException stack trace ---
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:775)
at android.support.v7.widget.Toolbar_NavigationOnClickEventDispatcher.n_onClick(Native Method)
at android.support.v7.widget.Toolbar_NavigationOnClickEventDispatcher.onClick(Toolbar_NavigationOnClickEventDispatcher.java:37)
at android.view.View.performClick(View.java:5698)
at android.view.View$PerformClick.run(View.java:22523)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

设备上的后退按钮运行正常,但我不知道为什么我在onBackPressed()上收到此错误。

1 个答案:

答案 0 :(得分:0)

你在活动中覆盖了这个方法吗?

public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Android.Resource.Id.Home:
                Finish();
                return true;
            default:
                return base.OnOptionsItemSelected(item);
        }

    }