Turning a button unclickable when scanning a QR code

207 Views Asked by At

I'm developing an app with a QR scanner.

I have three activities; "Skatte" which has a button that refers to another activity called "Skat1". "Skat1" contains a QR scanner. When you scan and get a positive result, it moves on to a third acitivy called "Skat1Resultat".

My problem is that I need to close the "Skat1" activity completely (or at least just make it impossible to enter) when you have scanned a QR code, which means that I also need to make the button on "Skatte" which refers to "Skat1" unclickable.

I've read about background services, threads and intents, but I still can't figure out how to do it. I've found a code which can change text on a third activity when you scan, but I need to do it with either a button or a clickable text.

This is the code for when a QR code has been scanned and it moves from "Skat1" to "Skat1Resultat".

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

  Toast toast = Toast.makeText(Skat1.this, contents, 9000);
  toast.show(); 
  startActivity(new Intent(Skat1.this, Skat1Resultat.class));
  finish();
  } 


} else if (resultCode == RESULT_CANCELED) {

 }

I though that finish(); could do the work and close the activity (I've also tried with onDestroy), but it's still possible to enter the activity when you click the button on "Skatte".

1

There are 1 best solutions below

0
On

You can use a static boolean flag with value false. Set it to true when you get "resultCode == RESULT_OK"

If the flag is false then the button is clickable If the flag is true then the button is not clickable.

if(flag)
{
button.setClickable(false);
}
else
{
button.setClickable(true);
}