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);
}
});