尝试在 SherlockFragment 中使用 WebView 制作应用程序,但是当我在单击 WebView 中的链接后按回时,我希望它返回上一页,但所发生的只是它关闭了应用程序。我想要的是返回上一页,如果不能再返回,则关闭应用程序。任何帮助都是极好的!
代码:
WebView myWebView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_4, container, false);
}
String mURL = "http://www.facebook.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("DetailFragment", "onCreate()");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v("DetailFragment", "onActivityCreated()");
if (savedInstanceState != null) {
mURL = savedInstanceState.getString("currentURL", "");
}
if (!mURL.trim().equalsIgnoreCase("")) {
myWebView = (WebView) getView().findViewById(R.id.webView);
myWebView.loadUrl(mURL.trim());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getView().setOnKeyListener( new OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
return myOnKeyDown(keyCode);
}
} );
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("currentURL", mURL);
}
public void setURLContent(String URL) {
mURL = URL;
}
public void updateURLContent(String URL) {
mURL = URL;
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(mURL.trim());
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context fragment_4) {
mContext = fragment_4;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://www.facebook.com")) {
// This is my web site, so do not override; let my WebView
// load the page
return false;
}
return false;
}
}
}
public boolean myOnKeyDown(int key_code) {
if ((key_code == KeyEvent.KEYCODE_BACK)
&& myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_4, menu);
return;
}
}
XML:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />