如何在Fragment中的WebView中添加“Go Back”function?

更新:解决了! 问题与我的Viewpager而不是WebView有关。

我正在尝试在我的WebView添加一个“Go Back”function,它位于Fragment 。 但我无法弄清楚如何:

 public final class TestFragment extends Fragment { static WebView mWeb; private View mContentView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.webview, null); mWeb = (WebView)mContentView.findViewById(R.id.webview); WebSettings settings = mWeb.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWeb.getSettings().setBuiltInZoomControls(false); mWeb.loadUrl("myurl..."); mWeb.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) { mWeb.goBack(); return true; } return false; } }); } } 

我也尝试过类似的东西:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) { mWeb.goBack(); return true; } return super.onKeyDown(keyCode, event); } 

另一种解决方案但同样的问

 @Override public void onBackPressed() { if(webView.canGoBack()) webView.goBack(); else super.onBackPressed(); } 

任何想法如何让这个工作?

Solutions Collecting From Web of "如何在Fragment中的WebView中添加“Go Back”function?"

也许它的机器人限制。 尝试使用处理程序执行此操作。

 public final class TestFragment extends Fragment { static WebView mWeb; private View mContentView; private Handler handler = new Handler(){ @Override public void handleMessage(Message message) { switch (message.what) { case 1:{ webViewGoBack(); }break; } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.webview, null); mWeb = (WebView)mContentView.findViewById(R.id.webview); WebSettings settings = mWeb.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWeb.getSettings().setBuiltInZoomControls(false); mWeb.loadUrl("myurl..."); mWeb.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && mWeb.canGoBack()) { handler.sendEmptyMessage(1); return true; } return false; } }); } private void webViewGoBack(){ mWeb.goBack(); } } 

实际上你不能直接在片段里面做。 可以在FragmentActivity覆盖onBackPressed 。 你能做的是:

  1. 覆盖活动内的onBackPressed
  2. 调用onBackPressed ,检查当前片段的实例是否是显示webview的实例。
  3. 如果是,请询问fragment是否可以返回webview
  4. 如果不是,请拨打super或任何您需要的电话

编辑:

  @Override public void onBackPressed() { Fragment webview = getSupportFragmentManager().findFragmentByTag("webview"); if (webview instanceof MyWebViewFragment) { boolean goback = ((MyWebViewFragment)webview).canGoBack(); if (!goback) super.onBackPressed(); } } 

你可以查看这段代码:

  webView.canGoBack(); webView.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && webView.canGoBack()) { webView.goBack(); return true; } return false; } }); 

WebViewActivity.java ,我添加了1个方法:

 @Override public void onBackPressed() { WebViewFragment fragment = (WebViewFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainer); if (fragment.canGoBack()) { fragment.goBack(); } else { super.onBackPressed(); } } 

WebViewFragment.java ,我添加了两个方法:

 public boolean canGoBack() { return mWebView.canGoBack(); } public void goBack() { mWebView.goBack(); } 

我的解决方案是片段我添加到公共方法

 public static boolean canGoBack(){ return mWebView.canGoBack(); } public static void goBack(){ mWebView.goBack(); } 

然后从我打电话的活动

 @Override public void onBackPressed() { if(webFragment.canGoBack()){ webFragment.goBack(); }else{ super.onBackPressed(); } } 

注意mwebview是静态的

你可以这样做:

  • Activity

     // Set WebView public void setWebView(WebView web) { this.web = web; } 
  • ActivityCreated()放入后的Web片段中:

    ((Your_Activity) getActivity()).setWebView(webView);

  • 不要忘记从onCreateView()设置webView ,如下所示:

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { webView = (WebView) inflater.inflate(R.layout.your_web_fragment, container, false); return web; } 

@OmidAmnivia的回答是正确的你的应用程序崩溃的解决方案是

 @Override public void onBackPressed() { if(webFragment.isInitialized && webFragment.canGoBack()){ webFragment.goBack(); }else{ super.onBackPressed(); } } 

您必须检查您的课程是否已初始化。

@ RomanBlack的回答给了我正确的想法,但由于我们使用kotlin,我不得不稍微调整答案。

 webView.setOnKeyListener { _, _, keyEvent -> if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK && !webView.canGoBack()) { false } else if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK && keyEvent.action == MotionEvent.ACTION_UP) { webView.goBack() true } else true } 

如果你想用返回来做,你必须添加如下内容:

 return@setOnKeyListener true 

首先回到片段中

  mContentView.setFocusableInTouchMode(true); mContentView.requestFocus(); mContentView.setOnKeyListener( new OnKeyListener() { @Override public boolean onKey( View v, int keyCode, KeyEvent event ) { if( keyCode == KeyEvent.KEYCODE_BACK ) { if (mWebView.canGoBack()) { mWebView.goBack(); retune false; } else { return true; } } return false; } } ); 

希望它会奏效。

我创建了一个简单的界面:

 public interface IOnBackPressed { boolean onBackPressed(); } 

在活动中:

 public class MyActivity extends Activity { @Override public void onBackPressed() { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container); if (!(fragment instanceof IOnBackPressed) || !((IOnBackPressed) fragment).onBackPressed()) { super.onBackPressed(); } } } 

在片段中:

 public class MyFragment extends Fragment implements IOnBackPressed { @Override public boolean onBackPressed() { if (webview.canGoBack()) { webview.goBack(); // backpress is not considered in the Activity return true; } else { // activity will act normal return false; } } }