I've got some problems while launching a Fragment if i performed a QR-Code scan via the ZXing 2.0 library.
I got this exception:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {fraiss.yucrowd/fraiss.yucrowd.activities.ScanQrCode}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState*
When i execute this code:
@Override
public void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.scan_qr_code);
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Log.d("MENU", "Scan result: " + contents + " as " + format);
// Load details for qr code
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
Fragment fragmentDetails = new FragmentDetails(
contents);
fragmentTransaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.fragment_main,
fragmentDetails);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.d("MENU", "Scan canceled");
}
}
}
I'am wondering why this happens. Is it not possible to launch a Fragment-Transaction at the end of a ActivityResult ?
Thx for your help in advance.
It is possible for onActivityResult
to be called before onResume
and you can't add new fragments before the Activity has atleast called onResume
.
Therefore you need to store the data you got in onActivityResult
, check for this in onResume
and perform your Fragment changes there.
This basically happens because your activity is paused, with onActivityResult
being called after onSaveInstanceState
, hence the message: 'Can not perform this action after onSaveInstanceState'. Save away the requestCode, resultCode and intent and check in onResume
if you have anything to work with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With