0

I am building an android app. In this snippet I am trying to call verifyKey method, which pops up a dialog to enter a key so that it can be confirmed if it is correct and allow a message to be sent.

Unfortunately the program does not return and run from where the verifyKey method was called. After closing the dialog I cannot find where the the program is in the code as Log.d("After verifyInvalid key", "!!!!!!!!!!!!!!!!!"); is not running. I have tried returning a boolean value from within the onClick in verifyKey but it is not allowing it from within a inner class. I am also getting a E/ViewRootImpl: sendUserActionEvent() mView == null.

Is there a simple way of returning this dialog, or how would i ensure the code returns while running from the location when the dialog was called.

this.testContact = new Contacts("testCon", "08xxxxxxx", "testkey", true);
    // assign on click listener to button
    send_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to send a message first an encryption key must be established
            if (testContact.getKeySet()) {
                verifyKey();
                if (keyChecker(verifyEnteredKey)) {
                    Log.d("Valid key", "!!!!!!!!!!!!!!!!!");
                    sendSms(testContact);
                } else {
                    Toast.makeText(getBaseContext(), "Invalid  Key",
                            Toast.LENGTH_LONG).show();
                }
                Log.d("After verifyInvalid key", "!!!!!!!!!!!!!!!!!");

                Log.d("OUTSDIDE KEY CEHCK", "outside");
            }
        }//on click
    });
 public boolean keyChecker(String entKey) {
    if (entKey.equals(testContact.getKey())) {
        Log.d("Valid key", "key confirmed");
        return true;
    }
    Log.d("inValid key", "key un-confirmed");
    return false;
}

public void verifyKey() {
    verifyEnteredKey = "";
    Builder builder = new Builder(SendMessage.this);
    builder.setTitle("Cloaked Key");
    // I'm using fragment here so I'm using getView() to provide ViewGroup
    // but you can provide here any other instance of ViewGroup from your Fragment / Activity
    View viewInflated = LayoutInflater.from(SendMessage.this).inflate(R.layout.key_entry_dialog, (ViewGroup) findViewById(android.R.id.content), false);
    // Set up the input
    final EditText input = viewInflated.findViewById(R.id.input);
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    builder.setView(viewInflated);

    // Set up the buttons
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            verifyEnteredKey = input.getText().toString();
            Log.d("ENTERED KEYY in dialog", verifyEnteredKey);
            // if(verifyEnteredKey.equals(testContact.getKey())){
            //    return true;
            // }
            // keyChecker(verifyEnteredKey);
            Log.d("Before dialog dismiss", verifyEnteredKey);
            dialog.dismiss();
            Log.d("afteer dialog dismiss", verifyEnteredKey);

        }

    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    Log.d("Before builder show", verifyEnteredKey);
    builder.show();
    Log.d("After builder show", verifyEnteredKey);


}
Ksofiac
  • 382
  • 1
  • 6
  • 21
MidnightP
  • 105
  • 1
  • 4
  • 12
  • 1
    You can not return from a method from inside a anonymous class . In this case either you should take flow further from the inner class method. You can call a method from inside inner class or you can use a callback . See [How to define callbacks](https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android). – ADM Mar 27 '18 at 18:27
  • Thank you that worked by calling`keyChecker(verifyEnteredKey);` from within the Dialog and then ` sendSms(testContact);`from within the keyChecker method. My only issue is that I would like to re-use this method ( to enter a key to send and to enter a key to view a message) and this way does not allow me to do so – MidnightP Mar 27 '18 at 18:46
  • 1
    For reusability of code you should follow Design patterns . Or you can simply start by creating a Utility class with generic object . Dig into this you will get the concept . Good luck. – ADM Mar 27 '18 at 18:49
  • Perfect, thank you very helpful . – MidnightP Mar 27 '18 at 19:03

1 Answers1

0

move this code block to onDismissListener https://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html

if (keyChecker(verifyEnteredKey)) {
                Log.d("Valid key", "!!!!!!!!!!!!!!!!!");
                sendSms(testContact);
            } else {
                Toast.makeText(getBaseContext(), "Invalid  Key",
                        Toast.LENGTH_LONG).show();
    }

essentially you are trying to read verifyEnteredKey before value is assigned in the callback

UDI
  • 1,602
  • 1
  • 14
  • 13