0

I'm trying to read a boarding pass barcode to inform the user if we have details on their flight and if so why not. I'm using AlertDialogs to communicate with the user as Toast notifications did not appear clearly enough. However they dismiss as soon as they are called without the user clicking ok.

How do I stop this? Is onActivityResult the wrong place to put this code?

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String boardingPassString = intent
                    .getStringExtra("SCAN_RESULT");
            Log.d("Scan Result", "contents: " + boardingPassString);
            String flightNumber = dataProcessor.decodeFlightNumber(boardingPassString);

            Builder dialogBuilder = new AlertDialog.Builder(this);
            String isFlightOld = isFlightOld(boardingPassString);
            if(isFlightOld.equals(CURRENT))
            {
                Log.d("Block", "Current");
                postData(flightNumber);
            }
            else if(isFlightOld.equals(TOO_NEW))
            {
                dialogBuilder.setTitle(R.string.dialog_title_new);

                dialogBuilder.setMessage(R.string.dialog_msg_new1 + flightNumber + R.string.dialog_msg_new2);
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();

                Log.d("Block", getResources().getString(R.string.dialog_title_new));
            }
            else if(isFlightOld.equals(OLD))
            {
                dialogBuilder.setTitle(R.string.dialog_title_old);
                dialogBuilder.setMessage(R.string.dialog_msg_old1 + flightNumber + R.string.dialog_msg_old2);
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();
                Log.d("Block", getResources().getString(R.string.dialog_title_old));
            }
            else
            {
                dialogBuilder.setTitle(R.string.dialog_title_error);
                dialogBuilder.setMessage(R.string.dialog_msg_error).show();
                dialogBuilder.setPositiveButton(android.R.string.ok, null);
                AlertDialog alert = dialogBuilder.create();
                alert.show();
                Log.d("Block", getResources().getString(R.string.dialog_title_error));
            }


        } else if (resultCode == RESULT_CANCELED) {
            Log.d("Scan Result", "RESULT_CANCELED");
        }
    }
}

Calling Code

scanButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // Testing shortcut
            // =================================
            // getXMLFlightDetails("US729");
            // ==================================

            // Uncomment to return Barcode scanning!!!
            // =============================================
            Intent intent = new Intent(getApplicationContext(),
                    CaptureActivity.class);
            intent.setAction("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "PDF417_MODE");
            intent.putExtra("SAVE_HISTORY", false);
            startActivityForResult(intent, 0);

        }
    });
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72

2 Answers2

0

Just try the following code, hopefully it should work. You need to pass the context in Dialog Builder. Then the Android OS will know which activity the dialog is attached to and where it should be popped.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String boardingPassString = intent
                    .getStringExtra("SCAN_RESULT");
            Log.d("Scan Result", "contents: " + boardingPassString);
            String flightNumber = dataProcessor.decodeFlightNumber(boardingPassString);

        Builder dialogBuilder = new AlertDialog.Builder(this);
        String isFlightOld = isFlightOld(boardingPassString);
        if(isFlightOld.equals(CURRENT))
        {
            Log.d("Block", "Current");
            postData(flightNumber);
        }
        else if(isFlightOld.equals(TOO_NEW))
        {
            displayAlert(R.string.dialog_msg_new1 + flightNumber + R.string.dialog_msg_new2);
            Log.d("Block", getResources().getString(R.string.dialog_title_new));
        }
        else if(isFlightOld.equals(OLD))
        {
            displayAlert(R.string.dialog_msg_old1 + flightNumber + R.string.dialog_msg_old2);
            Log.d("Block", getResources().getString(R.string.dialog_title_old));
        }
        else
        {
            displayAlert(R.string.dialog_msg_error);
            Log.d("Block", getResources().getString(R.string.dialog_title_error));
        }


    } else if (resultCode == RESULT_CANCELED) {
        Log.d("Scan Result", "RESULT_CANCELED");
    }
}
}



displayAlert(String message)
{

  AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyActivity.this);
  alertDialog.setTitle(getResources().getString(R.string.alert_title));
  alertDialog.setMessage(message);

  alertDialog.setPositiveButton(getResources().getString(R.string.ok),
    new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) 
      {
         //your action here
      }
  });


  alertDialog.setNegativeButton(getResources().getString(R.string.ok),
      new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
           // Your Action here      
      }
  });
  alertDialog.show();

}

If this does not solve your problem. Then you need to show ur entire activity code, as it may be possible that your activity is getting finished soon after the dialog is displayed to the screen.

Kailas
  • 7,350
  • 3
  • 47
  • 63
  • Same Problem as before. Weirdly both mine and your code works first time I build it. Every time after that it dismisses instantly. – Declan McKenna Jul 01 '14 at 12:27
  • Actually this and my code works as long as I rotate my phone quickly before the AlertDialog is called. My barcode scanner activity is landscape the trackActivity is portrait. The alertDialog being called within the wrong orientation appears to stop it being called or being dismissed if I'm in the middle of changing orientation. – Declan McKenna Jul 01 '14 at 12:48
  • Can't we have both activities using the same orientation? That should solve the issue then... just use the barcode scanner xml layout as landscape... hope that will help. – Kailas Jul 01 '14 at 13:32
  • This didn't work either. If I scan in portrait the problem is evaded but most people scan barcodes in landscape. – Declan McKenna Jul 01 '14 at 14:50
0

This turned out to be a multi-threading issue. The method postData used a HTTPResponse call so was not on the main thread. This method would finish a few milliseconds after tyhe dialog had been displayed and was what was dismissing the alertDialogs.

A simple join() call to get the main thread to wait for the postData method to do it's buisness before displaying the alertDialogs was all it took.

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72