0

I am trying to get the custom back button in Javascript on the webpage to work when the webpage is opened in the android webview.

The phone's standard back button on the bottom of the screen works fine. (the device's back button).

But how do I get the custom back buttom on the screen to work in the same way ?

I tried on the Javascript side; (mysettings.php)

<a href="#" onclick="history.back()"><img class="bla" src="images/bla.jpg"></a>


<a href="#" onclick="history.back();return false"><img class="bla" src="images/bla.jpg"></a>    


<a href="javascript: window.history.go(-1)"><img class="bla" src="images/bla.jpg"></a>      

A hyperlink works, but reloads the main page which I don't want. I just want to reshow the main page.

I could write some Java code here that I tried, but it doesn't seem to relate to the problem. E.g.

On the JAVA side (MainActivity.java)

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

Does anyone have experience with a custom javascript back button for an app webview ?

2
  • this looks more like Java than Javascript? Commented Feb 20, 2023 at 11:24
  • The first three <a href etc.. lines are Javascript from the mysettings.PHP file. The Java (@Override etc) is just to illustrate, that I have been trying in the MainActivity.java as well. But I can't find anything wrong there. Commented Feb 20, 2023 at 13:42

1 Answer 1

0

Okay, here it goes; (I also didn't want the first/main page to reload on my custom back button)

In HTML;

<a href="#" onclick="AndroidButton('Backbutton pressed')"><img src="bla.jpg"></a>

<script type="text/javascript">
    function AndroidButton(message1) {
        android.mybackbutton(message1);
    }
</script>

In Android studio (Java);

public class MainActivity<OneTime> extends AppCompatActivity {

    private Integer OneTime = 0;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = findViewById(R.id.web_view);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // etc.

        Uri uri = this.getIntent().getData();
        if (uri != null) {
            url = uri.toString();
        }

        // To prevent reload of the first page in the webView
        if (OneTime == 0) {
            webView.loadUrl(url);
            OneTime = 1;
        }

        webView.addJavascriptInterface(new JavaScriptInterface(), "android");

    } // End of onCreate

    private class JavaScriptInterface {
        @JavascriptInterface
        public void mybackbutton(String message1) {
             finish();
         }
    }

} // End of MainActivity

// When navigating from your first Javascript page to your next
// page by means of your own button or hyperlink, you will be able 
// to press your own back button on that new page and return to the
// first page without reloading it. I wanted it not to reload because 
// of the google map on it.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.