Edit: this bounce worked at the end. I tried to do this before, but actually I ran into a problem with my javascript. I had appBack () defined in document.onready. Just reinstall this function on the * window. * AppBack = function (). Made a business. Hope this helps someone.
public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { WebView.loadUrl("javascript:appBack()"); return true; } return super.onKeyDown(keyCode, event); }
In a web browser application, reverse navigation is handled using the special JS function appBack (). I am trying to figure out a way to intercept the android physical back button and call the javascript function instead. Here is my activity file. It is very simple, sets up web browsing and listens for a click on the back button. Currently, when the user clicks this physical button, he runs mWebView.goBack (). Here I would like to execute javascript: appBack ()
package com.stgeorgeplc.app; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; public class StGeorgePLCliteActivity extends Activity { WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.loadUrl("file:///android_asset/www/index.html"); } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } }
source share