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

时间:2012-05-17 07:14:44

标签: android android-fragments android-webview

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

我正在尝试向WebView内的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();
}

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

13 个答案:

答案 0 :(得分:45)

也许是它的机器人限制。尝试使用handler。

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();
    }
}

答案 1 :(得分:21)

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

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

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

答案 2 :(得分:16)

您可以查看以下代码:

    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;
        }
    });

答案 3 :(得分:6)

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();
}

答案 4 :(得分:3)

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

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是静态的

答案 5 :(得分:2)

您可以通过以下方式执行此操作:

    <{> 1}}中的
  • Activity
  • // Set WebView public void setWebView(WebView web) { this.web = web; } 之后的网络片段中的
  • ActivityCreated()

  • 不要忘记从这些((Your_Activity) getActivity()).setWebView(webView);设置webView

    onCreateView()

答案 6 :(得分:1)

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

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

您必须检查您的班级是否已初始化。

答案 7 :(得分:1)

@ 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

答案 8 :(得分:1)

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

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;
        }
    }
}

答案 9 :(得分:1)

这就是我在应用程序中所做的事情。我消耗了新闻发布事件,直到可以返回Web视图。一旦网络视图无法返回,我会向用户显示提示,如果他继续按回去,则应用程序将退出。

当webview无法返回时,它将为用户提供留在您应用中的机会。我觉得它更加用户友好:

          //time passed between two back presses.
          private val TIME_INTERVAL = 200 
           // variable to keep track of last back press
          private var mBackPressed: Long = 0


        webView!!.requestFocus()
        webView.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.action == MotionEvent.ACTION_UP
                  ) {
                if(webView.canGoBack()) {
                    //go back in previous page
                    webView.goBack()
                    return@OnKeyListener true
                }
                else
                {
                    if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
                    {   // dont consume back press and pass to super
                        return@OnKeyListener false
                    }
                    else {
                        // show hint for double back press
                        Toast.makeText(context, " Double Tap back button to exit the demo", Toast.LENGTH_SHORT).show();
                        mBackPressed = System.currentTimeMillis();
                        return@OnKeyListener true
                    }
                }
            }
            return@OnKeyListener false

        })

答案 10 :(得分:1)

BackPressedDispatcher有一个简单的方法

片段:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val callback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            if(webView.canGoBack()){
               webView.goBack()
            } else {
                isEnabled = false
                requireActivity().onBackPressed()
            }
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(this,callback)
}

活动:

override fun onBackPressed() {
    val fragment = supportFragmentManager.findFragmentByTag("WebViewFragment")
    if (WebViewFragment::class.java.isInstance(fragment)) {
        if (onBackPressedDispatcher.hasEnabledCallbacks()) {
            onBackPressedDispatcher.onBackPressed()
            return
        }
        super.onBackPressed()
    }
}

也许这些代码可以改进,但是对我来说效果很好。有关更多信息,请访问here

答案 11 :(得分:0)

首先回到片段

 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;
  }
 } );
希望它能起作用。

答案 12 :(得分:0)

这对我来说很有效

public class FantasyFragment extends Fragment  implements SwipeRefreshLayout.OnRefreshListener {

WebView webview;
SwipeRefreshLayout swipeLayout;
String currentUrl="https://www.stackoverflow.com/";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_stadium, container, false);


    swipeLayout = (SwipeRefreshLayout) root.findViewById(R.id.swipescreen);
    swipeLayout.setOnRefreshListener(this);

    return root;
}

@Override
public void onStart() {
    super.onStart();
    LoadWeb();
}

public void LoadWeb() {



    webview = (WebView) getActivity().findViewById(R.id.webview786);
    swipeLayout.setRefreshing(true);
    webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webview.getSettings().setAppCacheEnabled(true);

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setDatabaseEnabled(true);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webSettings.setUseWideViewPort(true);
    webSettings.setSavePassword(true);
    webSettings.setSaveFormData(true);
    webSettings.setEnableSmoothTransition(true);

    webview.loadUrl(currentUrl);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Snackbar.make(view, "Connection Error", Snackbar.LENGTH_LONG)
                    .setAction("Retry", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LoadWeb();
                        }
                    }).show();
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            swipeLayout.setRefreshing(false);
            currentUrl = url;
            super.onPageFinished(view, url);
        }
    });

    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;
        }
    });
}
@Override
public void onRefresh() {
    LoadWeb();
}
}