Detect use of Android back button using JavaScript

后端 未结 2 1896
走了就别回头了
走了就别回头了 2021-02-20 05:54

My site has breadcrumbs which highlight which stage in a process the user has reached. The breadcrumbs rely on the browser history to tell which stage should be highlighted when

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-20 06:20

    You can put check on inside onBackPressed method of the activity and before calling super.onBackPressed(); trigger the method to call javascript method.

    for e.g:

    override fun onBackPressed() {
        if (handleBackPress) {
            myWebView.loadUrl("javascript:backButtonPressed()")
        }else {
            super.onBackPressed()
        }
    }
    

    in the above example if handleBackPress boolean variable is true, then it will try to call backButtonPressed() method of javascript file in the webview.

    Let me know if you need any explanation or help.

提交回复
热议问题