私はwebviewフラグメントを持っています。私は前のページに戻るために、組み込みのAndroidの戻るボタンを使用したいと思います。この断片がロードされている私の主な活動でWebビューの前のページに戻る
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;
}
}
、私は)これを行うとthis.getActivity.onBackPressedを(呼び出すことにより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();
}
}
}
をオーバーライドしています。フラグメントでは、戻るボタンは何もしませんが、もしあれば前のページには行きません。断片
活動のため@Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
if (webview instanceof MyWebViewFragment) {
boolean goback = ((MyWebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}
、 については
あなたが追加することができるのonCreateメソッドの外にして、クラス内のメソッドをonKeyDownメソッド置き、あなたのrootViewためsetOnKeyListenerを設定しないでください戻るボタンが押されたときに停止点を追跡するために 'onBackPressed()'の中でデバッグする 'Log.d()' –
[WebViewで戻るボタンが押された場合、前のページに戻る方法]の複製がありますか?(http://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-戻るボタンが押されたままになっています) –