返回上一页后退键按Android WebView Fragment

时间:2016-07-20 07:08:37

标签: android android-fragments webview

我正在使用导航抽屉活动并有5个片段,每个片段都使用WebView打开网页。在我刚刚使用一个MainActivity之前,它只是打开了一个网站,并且能够轻松地返回到之前打开的页面,但现在我找不到任何可以让我这样做的东西了。我目前在我的片段中使用此代码,我使用代码从here返回,每当我打开片段时,应用程序只需强制关闭。

package com.science.s11;

import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class ask extends Fragment {

public WebView mWebView;

public ProgressBar progressBar;
public LinearLayout layoutProgress;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.ampproject, container, false);
    mWebView = (WebView) v.findViewById(R.id.aboutuswebViewask);
    progressBar = (ProgressBar) v.findViewById(R.id.progressBarask);
    layoutProgress = (LinearLayout) v.findViewById(R.id.layoutProgressask);
    mWebView.setVisibility(View.GONE);
    mWebView.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
            {
                WebView webView = (WebView) v;

                switch(keyCode)
                {
                    case KeyEvent.KEYCODE_BACK:
                        if(webView.canGoBack())
                        {
                            webView.goBack();
                            return true;
                        }
                        break;
                }
            }

            return false;
        }
    });
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    WebSettings settings = mWebView.getSettings();
    settings.setSupportMultipleWindows(true);
    settings.setBuiltInZoomControls(false);
    settings.setSupportZoom(false);
    settings.setDisplayZoomControls(false);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mWebView.setVisibility(View.VISIBLE);
            layoutProgress.setVisibility(View.GONE);
            progressBar.setIndeterminate(true);
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            layoutProgress.setVisibility(View.VISIBLE);
            progressBar.setIndeterminate(false);
            super.onPageStarted(view, url, favicon);
        }


    });



    if (isOnline()) {
        mWebView.loadUrl("http://google.com");
    } else {
        mWebView.loadUrl("file:///android_asset/cti.html");
    }
    return v;
}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}}

你能帮帮我吗?

3 个答案:

答案 0 :(得分:2)

试试这个,它很简单,工作正常。

在所有5个片段中,将变量设置为public static:

public static WebView mWebView;

在您的主要活动中:

@Override
public void onBackPressed() {
    if (Fragment_1.mWebView!= null) {
        if (Fragment_1.mWebView.canGoBack()) {
            Fragment_1.mWebView.goBack();
        } else {
           // do when mWebView cant go back anymore
        }
    }
    if (Fragment_2.mWebView!= null){
        if (Fragment_2.mWebView.canGoBack()) {
            Fragment_2.mWebView.goBack();
        } else {
             // do when mWebView cant go back anymore
        }
    }
    if (Fragment_3.mWebView!= null){
        if (Fragment_3.mWebView.canGoBack()) {
            Fragment_3.mWebView.goBack();
        } else {
             // do when mWebView cant go back anymore
        }
    }
    if (Fragment_4.mWebView!= null){
        if (Fragment_4.mWebView.canGoBack()) {
            Fragment_4.mWebView.goBack();
        } else {
            // do when mWebView cant go back anymore
        }
    }
     //and so on......
}

答案 1 :(得分:0)

试试这个:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

答案 2 :(得分:0)

你可以制作像这样的基础片段

select (case when min(id) > 0 then 1 else min(id) + 1 end)
from table1 t
where not exists (select 1
                  from table1 t2
                  where t2.id = t.id + 1
                 );

}

并覆盖片段中的onBackPressed(),如下所示

公共类TestFragment扩展了BaseFragments {

public abstract class BaseFragments extends Fragment {

abstract public int setContentView();

public void replaceFragment(Context context, Fragment fragment) {
    ((BaseActivity) context).getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();
}

@Override
public void onResume() {

    super.onResume();
    // preferencesUtility.setuserData(context, userdata);
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP
                    && keyCode == KeyEvent.KEYCODE_BACK) {

                // Toast.makeText(context, "clicked", Toast.LENGTH_LONG)
                // .show();
                backPressed();

                return true;

            }

            return false;
        }

    });
}


protected abstract void backPressed();

}