-2

I have an app that scans QR code. And it to show in another activity. How to pass QR Code text to another activity? Or save it in memory?

MainActivity

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
    private ZXingScannerView zXingScannerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        zXingScannerView = new ZXingScannerView(getApplicationContext());
        setContentView(zXingScannerView);
        zXingScannerView.setResultHandler(this);
        zXingScannerView.startCamera();
    }    
    @Override
    protected void onPause() {
        super.onPause();
        zXingScannerView.stopCamera();
    }
    @Override
    public void handleResult(Result result) {
        Toast.makeText(getApplicationContext(), result.getText(),Toast.LENGTH_SHORT).show();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Result");
        builder.setMessage(result.getText());
        AlertDialog alertDialog = builder.create();
        alertDialog.show();         
        zXingScannerView.resumeCameraPreview(this);

    }
}
Alina
  • 424
  • 1
  • 4
  • 17
  • 1
    You can pass data to your `Activity` through the `Intent` using `Extras`, [here's an example](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application). – LS_ Feb 27 '18 at 09:31

1 Answers1

1

Call method startActivityForResult(new Intent(this, YourCallbackActivity.class), 1001); Use setResult() method for get call back in another activity.

@Override
public void handleResult (Result result) {
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result",String.valueOf(rawResult));
    setResult(Activity.RESULT_OK,returnIntent);
    finish();
}

now

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1001) {
        if (resultCode == RESULT_OK && data != null) {
            String result = data.getStringExtra("result");
            //do whatever you want
        }
    }
}

I already did, hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49