0

I have been struggling with this issue for about a day now. When I press the back button on my phone it runs my code backwards. Can anyone perhaps tell me why this happens and how I will fix it??

public void onBackPressed() {
    displayToast("Pressed Back"); // Executed last
    superWebView.evaluateJavascript("window.sessionStorage.getItem('windowActive')",
            new ValueCallback<String>()  {
                @Override
                public void onReceiveValue(String value)
                {
                    if ("logo.html".equals(value)) { // Gets skipped
                        displayToast("Press back again to exit");
                        new AlertDialog.Builder(mAppContext)
                                .setTitle("Closing Program")
                                .setMessage("Are you sure you want to exit Vertex?")
                                .setPositiveButton("Yes", (dialog, which) -> finish())
                                .setNegativeButton("No", null)
                                .show();
                    } else {
                        displayToast("Default"); // This executes first
                        superWebView.loadUrl("javascript:goBack()");
                    }
                }
            });


}
gioravered
  • 1,758
  • 3
  • 19
  • 30
  • The only reason the code is skipping the if statement is if the condition is false. So you need to check why "logo.html".equals(value) returns false – gioravered Apr 07 '22 at 07:47
  • **TIP**: try to use `Breaking Points`, or `Logs` instead of `Toasts`. This will give instant results and will allow you to test variables' values. – Hamza Sharaf Apr 07 '22 at 07:47
  • I am using the toasts just to see what is executing and I created an alert to check the value that gets returned and it returns logo.html, I even checked the type that I get back and it says java.lang.String – Martin De Beer Apr 07 '22 at 07:49
  • Try handling the back press following this method: https://stackoverflow.com/a/17652491/2649154 – gioravered Apr 07 '22 at 09:01
  • gioravered - Thank you, this link saved me – Martin De Beer Apr 07 '22 at 09:50

0 Answers0