c# - Android application throwing: Java.Lang.IllegalStateException, with message: Can not perform this action after onSaveInstanceState -
to shorten problem: developing android application requires users log in, , since multiple users logged in @ same time, want cycle between authenticated users using nfc touch cards. works fine except after using zxing.mobile
bar-code scanner, when code returns scanning bar-code, , tries push page model, particular exception being thrown java.lang.illegalstateexception: can not perform action after onsaveinstancestate
. please note using xamarin.forms, freshmvvm, zxing.mobile, , of course c#.
fragments of code used:
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>
the above code used enable application launch using nfc tags. splashactivity
launches 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 simplify question. }
above code shown know how using nfc touch event.
code open bar-code scanner main page model:
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);
from scan service:
public async task<string> camerascanasync() { //autofocus code omitted simplify question result result = await _mobilebarcodescanner.scan(new mobilebarcodescanningoptions { possibleformats = _listofbarcodeformats }).configureawait(false); return result == null ? string.empty : result.text; }
edit: code containing push page model method:
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(); }
this code fired directly after returning scanning bar-code.
end edit
all of works after nfc card being touched , application launched, until hitting next line of code. after bar-code returned scanner:
await coremethods.pushpagemodel<secondarypagemodel>(response).configureawait(true);
the exception being thrown right here. debugged code check what's going on. when camera open, first fires mainactivity onsaveinstancestate
event, after scanning bar-code, mainactivity onresume
> mainactivity onpostresume
events fired in order. pushpagemodel
method called. note when manually enter bar-code in relative field, works fine, it's scanner throws exception.
i have searched solutions here in so. found answers says opt-out base.onsaveinstancestate()
line, tried no luck, answer said enter junk values work around issue, tried no luck well. tried different launch modes in androidmanifest file singletop
or singletask
or singleinstance
no luck well.
i glad given. in advance.
mainactivity running multiple instances when switching using nfc, add mainactivity launchmode singletask, when launch using nfc clear task , create new one. way specify activity flags using xamarin attributes, better way adding them in manifest.xml
Comments
Post a Comment