我有一个webview片段,我想使用内置的Android后退按钮返回上一页。
package kyfb.android.kyfb.com.kyfb;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
/**
* Created by Adam Rayborn on 8/25/14.
*/
public class MyKYFB extends Fragment {
public WebView web;
private ProgressBar progressBar;
private String url;
private static final String SP_LAST_WEBVIEW_URL = "com.app.kyfb.LASTWEBVIEWURL";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_my_kyfb, null);
url = "https://my.kyfb.com";
web = (WebView)rootView.findViewById(R.id.kyfb_web);
progressBar = (ProgressBar)rootView.findViewById(R.id.prgPageLoading);
web.getSettings().setDomStorageEnabled(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
SharedPreferences sharedPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
String savedUrl = sharedPrefs.getString("URL", null);
if (savedUrl != null) {
web.loadUrl(savedUrl);
}
else {
web.loadUrl(url);
}
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView webView, int progress) {
getActivity().setProgress(progress * 100);
progressBar.setProgress(progress);
}
});
// this.getView().setFocusableInTouchMode(true);
rootView.setFocusableInTouchMode(true);
web.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(web, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(web, url);
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("URL", web.getUrl());
editor.apply();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi") || url.endsWith(".flv") || url.endsWith(".pdf")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i); //warning no error handling will cause force close if no media player on phone.
return true;
}
view.loadUrl(url);
return true;
}
});
this.getActivity().onBackPressed();
return rootView;
}
}
在我加载此片段的主要活动中,我已覆盖onBackPressed()
@Override
public void onBackPressed() {
Fragment webview = getFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyKYFB) {
boolean goback = ((MyKYFB) webview).web.canGoBack();
if (!goback) {
super.onBackPressed();
} else {
((MyKYFB) webview).web.goBack();
}
}
}
通过这样做并调用this.getActivity.onBackPressed();在片段中,后退按钮不做任何事情,但如果有的话,它不会转到上一页。
答案 0 :(得分:0)
For Fragment
@Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}
对于活动, 不要为rootView设置setOnKeyListener,只需在onCreate方法之外和类内放置onKeyDown方法
imports ....;
public WebView myWebView;
public class MainActivity extends AppcompatActivity {
..
protected void onCreate(Bundle savedInstanceState) {
WebView myWebView = (WebView) findViewById(R.id.webview);
..}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}