Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching a Fragment after ZXing 2.0 QR-Code Scan

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.

like image 276
Mario Fraiß Avatar asked Mar 19 '12 04:03

Mario Fraiß


2 Answers

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.

like image 114
Blundell Avatar answered Oct 19 '22 10:10

Blundell


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.

like image 43
Joakim Berglund Avatar answered Oct 19 '22 09:10

Joakim Berglund