How to start an action using Intent and pass a variable in a new action?

So now I am using the zxing barcode scanner in my application. Here is a sample code (general):

if(position == 0){
            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) {
                contents = intent.getStringExtra("SCAN_RESULT");
                format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
                Intent i = new Intent(Main.this, BarcodeScanner.class);
                startActivity(i);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

Therefore, at startup, BarcodeScanner.classI also want to pass contentsinto it. How should I do it?

+3
source share
2 answers

Use the Bundle to transfer data from one activity to another. In your case, you will need to do something like:

        Intent intent = new Intent(Main.this,BarcodeScanner.class);

        //load the intent with a key "content" and assign it value to content            
        intent.putExtra("content",contents);

        //launch the BarcodeScanner activity and send the intent along with it
        //note that content  is passed in as well             
        startActivity(intent);

The information is stored in the 'Bundle' object, which is inside the Intent - the package is created when you call the putExtras () method of the Intent object

+6
source

"SCAN_MODE" , putExtra("some key", contents) startActivity(), BarcodeScanner this.getIntent().getStringExtra("some key")

+1

Source: https://habr.com/ru/post/1772293/


All Articles