Android应用程序抛出:Java.Lang.IllegalStateException,带有消息:onSaveInstanceState

时间:2016-11-10 10:30:34

标签: c# android xamarin zxing freshmvvm

缩短问题:我正在开发一个需要用户登录的Android应用程序,由于多个用户可以同时登录,我想在经过身份验证的用户之间进行循环NFC触摸卡。一切正常,除非在使用ZXing.Mobile条形码扫描器之后,当代码从扫描任何条形码返回时,并尝试推送页面模型,这个特殊异常被抛出Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState。请注意我使用的是Xamarin.Forms,FreshMVVM,ZXing.Mobile,当然还有C#。

使用的代码片段:

的AndroidManifest.xml:

<activity android:name="com.name.SplashActivity">
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/com.name.nfc" />
    </intent-filter>
</activity>
<activity android:name="com.name.MainActivity">
</activity>

以上代码用于使应用程序能够使用NFC标签启动。 SplashActivity启动MainActivity

SplashActivity.cs:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    StartActivity(typeof(MainActivity));
}

protected override void OnResume()
{
    base.OnResume();

    if (NfcAdapter.ActionNdefDiscovered == Intent.Action)
    {
        ProcessIntent(Intent);
    }
}

protected override void OnNewIntent(Intent intent)
{
    Intent = intent;
}

public void ProcessIntent(Intent intent)
{
    //Code omitted to simplify the question.
}

上面的代码只是为了了解我如何使用NFC触摸事件。

从主页模型中打开条形码扫描器的代码:

public ICommand OpenCameraCommand => new Command(async () =>
{
    IsAvailable = false;
    ((Command) OpenCameraCommand).ChangeCanExecute();
    string checkBarcode = await _scanService.CameraScanAsync().ConfigureAwait(true);
    if (!string.IsNullOrWhiteSpace(checkBarcode))
    {
        Barcode = checkBarcode;
    }
    IsAvailable = true;
}, () => IsAvailable);

来自扫描服务:

public async Task<string> CameraScanAsync()
{
    //AutoFocus code omitted to simplify the question

    Result result = await _mobileBarcodeScanner.Scan(new MobileBarcodeScanningOptions { PossibleFormats = _listOfBarcodeFormats }).ConfigureAwait(false);

    return result == null ? string.Empty : result.Text;
}

修改: 包含推送页面模型方法的代码:

switch (response.Status)
{
    case Case.Second:
        await CoreMethods.PushPageModel<SecondaryPageModel>(response).ConfigureAwait(true);
        Barcode = string.Empty;
        return;
    case Case.Third:
        await CoreMethods.PushPageModel<ThirdPageModel>(response).ConfigureAwait(true);
        Barcode = string.Empty;
        return;
    case Case.Fourth:
        await CoreMethods.PushPageModel<FourthPageModel>(response).ConfigureAwait(true);
        Barcode = string.Empty;
        return;
    case Case.Invalid:
        break;
    default:
        throw new InvalidOperationException();
}

从扫描条形码返回后直接触发此代码 结束编辑

所有这些都在触摸NFC卡并启动应用程序后工作,直到达到下一行代码。从扫描仪返回条形码后:
await CoreMethods.PushPageModel<SecondaryPageModel>(response).ConfigureAwait(true);

正在抛出异常。我调试了我的代码来检查发生了什么。当相机打开时,它会在成功扫描条形码MainActivity OnSaveInstanceState&gt;成功后触发MainActivity OnResume事件。按此顺序触发MainActivity OnPostResume个事件。然后调用PushPageModel方法。 请注意,当我在相对字段中手动输入条形码时,一切正常,它只是引发此异常的扫描程序。

我在SO搜索了解决方案。我找到了一些答案,说选择退出base.OnSaveInstanceState()行,我试着没有运气,另一个答案说要输入垃圾值来解决这个问题,试着没有运气。我在AndroidManifest文件中尝试了不同的启动模式,例如singleTopsingleTasksingleInstance,但也没有运气。

我会很高兴得到任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

当您使用NFC切换时,MainActivity正在运行多个实例,将MainMctivity LaunchMode添加为SingleTask,然后当您使用NFC启动时清除任务并创建一个新任务。指定Activity标志的一个好方法是使用Xamarin属性,这比在Manifest.xml中添加它们更好的方法