1

为了缩短问题:我正在开发一个需要用户登录的 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事件,在成功扫描条形码后,MainActivity OnResume>MainActivity OnPostResume事件按顺序触发。然后PushPageModel调用该方法。请注意,当我在相关字段中手动输入条形码时,一切正常,只是扫描仪引发了此异常。

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

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

4

1 回答 1

1

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

于 2016-11-26T13:25:53.307 回答