0

I am displaying a progress dialog while some background operation is performed. I am using Eventbus to post back to the fragment that's displaying the dialog. I'm using a variable to hold a reference to the dialog. When I test this program using Genymotion emulators, it works perfectly. When I test it on a real device, the variables go null.

public ProgressDialog mAuthorizeProgress;

First, the User is presented with a dialog where they enter a 4 digit code. When they click "OK", the 4 digit code is processed (async) and the ProgressDialog(mAuthorizeProgress) is displayed.

After the User click OK, the progress dialog is displayed:

protected void sendProtectedCommand(OmniCommand cmd) {
    mPendingCommand = cmd;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    if (prefs.getBoolean("pref_key_model_code_required",false)) {
        AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
        alert.setTitle("Enter Code");
        final EditText input = new EditText(mContext);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        input.setRawInputType(Configuration.KEYBOARD_12KEY);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Put actions for OK button here
                int code = 0;
                try {
                    code = Integer.parseInt(input.getText().toString());
                } catch (Exception e) {
                    Log.e(tag, "Exception="+e.toString());
                }
                sendRequest(Omni.REQ_SECURITY_VALIDATION, OmniRequest.securityValidation(code));
                showAuthorizeProgress();
                if (mAuthorizeProgress !=null) {
                    Log.d(tag, "mAuthorizeProgress is NOT null right after being shown.");
                } else {
                    Log.d(tag, "mAuthorizeProgress is null right after being shown.");
                }
                mHandler.postDelayed(() -> {
                    Log.d(tag, "authorization timeout is over.");
                    hideAuthorizeProgress("Authorization Timed Out");
                }, AUTHORIZATION_PROGRESS_TIMEOUT);
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Put actions for CANCEL button here, or leave in blank
                render(); // should reset spinner
            }
        });
        AlertDialog alertToShow = alert.create();
        alertToShow.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        alertToShow.show();
    } else {
        sendCommand(cmd);
    }
}

This is the function that shows the progress dialog:

public void showAuthorizeProgress() {
    Log.d(tag, "showAuthorizeProgress()");
    mPendingCodeResponse = true;
    mAuthorizeProgress = ProgressDialog.show(
            mContext,
            "Security Validation",
            "Validating Provided User Code",
            true, false);
    if (mAuthorizeProgress !=null) {
        Log.d(tag, "mAuthorizeProgress is NOT null right after creation.");
    } else {
        Log.d(tag, "mAuthorizeProgress is null right after creation.");
    }
    if (mPendingCodeResponse) {
        Log.d(tag, "mPendingCodeResponse is TRUE right after creation.");
    } else {
        Log.d(tag, "mPendingCodeResponse is FALSE right after creation.");
    }
}

Here is a snippet of the dismiss code:

public void onEventMainThread(E.StorageChanged e) {
            ...
            if (mAuthorizeProgress !=null) {
                Log.d(tag, "mAuthorizeProgress is NOT null right before being hidden.(s)");
            } else {
                Log.d(tag, "mAuthorizeProgress is null right before being hidden.(s)");
            }
            String msg = "Authorization Success!";
            Log.d(tag, "hideAuthorizeProgress(): msg="+msg);
            mPendingCodeResponse = false;
            if (mAuthorizeProgress !=null) {
                mAuthorizeProgress.dismiss();
            } else {
                Log.d(tag, "mAuthorizeProgress is null.");
            }
            ...

Here is the log from the real device:

12-18 11:30:11.034 25448 25448 D GenericCard: mAuthorizeProgress is null right before being hidden.(s)
12-18 11:30:11.034 25448 25448 D GenericCard: hideAuthorizeProgress(): msg=Authorization Success!
12-18 11:30:11.034 25448 25448 D GenericCard: mAuthorizeProgress is null.

Here is the log from the emulator:

12-18 12:38:09.914 12560-12560/com.app D/GenericCard: mAuthorizeProgress is NOT null right before being hidden.(s)
12-18 12:38:09.914 12560-12560/com.app D/GenericCard: hideAuthorizeProgress(): msg=Authorization Success!

And of course, the progress dialog is dismissed in the emulator, but not on the real device.

I'm at a complete loss. Any advice on how to debug this issue is greatly appreciated.

tunneling
  • 527
  • 6
  • 21
  • 1
    We need to see the code where `mAuthorizeProgress` is set, not the logging code... – NoChinDeluxe Dec 18 '15 at 17:44
  • I would *strongly* encourage you to use `DialogFragment`, and I don't get the sense that you are doing that here. With regards to this issue, either you are setting `mAuthorizeProgress` to `null` somewhere, or whatever class has `mAuthorizeProgress` is being recreated (e.g., lifecycle stuff). – CommonsWare Dec 20 '15 at 17:10
  • would you expect "lifecycle stuff" to behave differently between a real device and the emulator? – tunneling Dec 20 '15 at 20:22

0 Answers0