0

I have a webview app that almost all the contend is loaded using jQuery AJAX function. My problem here is when i click on mobile back button it doesn't go instead it close the app or reload. From my website i created a jquery function to go back and is working fine, so what i thought of doing is execute that function inside android code with previouse save url. So i created JavascriptInterface, to get previouse url from jQuery FSAndroid.setAsycUrlInterface(url) but my problem is how do i execute jsBackAsync(from, url) inside my android code in onKeyDown?

Here is my JavascriptInterface class

public class AsyncInterface {
        Context iContext;
        AsyncInterface(Context contex) {
            iContext = contex;
        }

        @JavascriptInterface 
        public void setAsycUrlInterface(String geturl) {
            SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString("lastAsyncUrl", geturl);
            edit.commit();
        }
    }

Here is onKeyDown method

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && shopwebview.canGoBack()) {
        shopwebview.goBack();
        /* Something like this i want to do
        SharedPreferences asyncprefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        String lastAsyncUrlTab = asyncprefs.getString("lastAsyncUrl", "");
        if(!lastAsyncUrlTab.equals("")) {
            jsBackAsync("Android", lastAsyncUrlTab);
        }
        */
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Here is my jquery code

$(document).ready(function(){
    $('a.AsyncUrl').click(function(){
        var url = $(this).attr('href');
        FSAndroid.setAsycUrlInterface(url);
    });

    function jsBackAsync(from="button", url){
        if(from == 'Android'){
            /*Clear the back url or set new one here*/
            FSAndroid.setAsycUrlInterface(null);
        }
        $('#PageSet').load(url);
    }
});
Peter
  • 1,860
  • 2
  • 18
  • 47
  • I think this is what you want. https://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-back-button-is-pressed-in-webview – Vivek Mishra Oct 15 '18 at 06:45
  • @VivekMishra is not what am actually looking for, I want a way to execute my custom back jquery function from `onKeyDown` because default back won't work for me as am loading my page content using Ajax – Peter Oct 15 '18 at 06:53

0 Answers0