0

hi i am currently working on QR code scanner. I want to ask if it is possible to open another activity after the QR code have been scanned rather than getting it displayed as a toast? below is my scanner file code

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mtexthello=(TextView)findViewById(R.id.textview_hello);
    scanbtn=(Button)findViewById(R.id.btn1) ;
    final  Activity activity=this;

    scanbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator l=new IntentIntegrator(activity);
            l.setDesiredBarcodeFormats(l.QR_CODE_TYPES);
            l.setPrompt("scan");
            l.setCameraId(0);
            l.setBeepEnabled(false);
            l.setBarcodeImageEnabled(false);
            l.initiateScan();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    IntentResult res= IntentIntegrator.parseActivityResult(requestCode, resultCode,data);
    if(res!=null)
     if(res.getContents()==null)
     {
         Toast.makeText(this,"u cancelled scanning",Toast.LENGTH_LONG).show();

     }
     else
     {

         Intent intent = new Intent(MainActivity.this, LoginActivity.class);
         startActivity(intent);
        // Toast.makeText(this,res.getContents(),Toast.LENGTH_LONG).show();

     }




    super.onActivityResult(requestCode, resultCode, data);
    }
}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
WiLc0
  • 11
  • 3
  • 1
    Can you share your code? and please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Dor Jun 12 '19 at 15:32
  • yes sure shared it u can check it now – WiLc0 Jun 12 '19 at 15:40
  • I think you want to pass the data to the next activity. – diffuse Jun 12 '19 at 15:44
  • no.. i just want to open a new activity in which there is a login form thats it in simple word i want to redirect to login activity when the qr code is scanned! – WiLc0 Jun 12 '19 at 15:46
  • if it is possible to open a new activity, after finishing the scan of the QR. But do you have a problem when doing it? – Juanes30 Jun 12 '19 at 15:49
  • my app crashes when i scan the qrcode @JuanE.LondoñoT. – WiLc0 Jun 12 '19 at 15:51
  • when is your app crashing? when trying to open a new activity? or what error you get, if you want to share the stacktrace that you get in android studio and you can help. – Juanes30 Jun 12 '19 at 15:53
  • @JuanE.LondoñoT. when i scan the qr code with toast message it scans it successfully but when i scan it with "intent" in the onActivityResult fucntion it crashes – WiLc0 Jun 12 '19 at 17:04
  • what error do you get when executing the intent? – Juanes30 Jun 12 '19 at 17:26
  • app stop working@JuanE.LondoñoT. – WiLc0 Jun 12 '19 at 17:34
  • @JuanE.LondoñoT. did it!! just one image view was doing problem btw thanks to all of u.. – WiLc0 Jun 12 '19 at 18:45

1 Answers1

0

At a first reading it sounds as if the issue is how you're attempting to start the next activity, if not the activity itself.

Start a new app, and use a simple Button as the trigger for the new activity. Once you have the syntax and requirements correct there, apply what you've learned to the QR app.

If you're able to get the Toast to display from your existing code, then your difficulty has nothing to do with the QR code.

Maybe what you really need is to look at something like this (Start an Activity with a parameter) where you pass a parameter to the second activity.

Fachtna Roe
  • 116
  • 2