如何在Fragment中的WebView中添加Go Back功能? (一式两份)

时间:2017-01-22 18:07:03

标签: android

当前代码无效,我想在webview中添加goback功能,当我在移动设备上按下后退按钮时。我该如何解决?以下是我目前的片段。指导我:)发现了一些相同的问题,但没有得到答案..

解决了!这就是我发现的事情!!

///MY FRAGMENT

}

public static WebView getWebView(){
return webView;  
}

}


----------------------


///MY ACTIVITY



@Override
	public void onBackPressed() {
		
		WebView webView = fbfragment.getWebView();

		if (webView.canGoBack()) {
			webView.goBack();

		} else {

			Builder builder = new AlertDialog.Builder(this);

			builder.setTitle("Exit").setMessage("Are you sure you want to exit?").setCancelable(false)
					.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							finish();
						}
					}).setNegativeButton("No", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int id) {
						}
					});

			AlertDialog alert = builder.create();
			alert.setCancelable(false);
			alert.show();

		}

	}
}

       package XXXX.XXXXXXXXXX.XXXXXXXXXXXX.com;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.CookieManager;
    import android.webkit.CookieSyncManager;
    import android.webkit.JavascriptInterface;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import XXXX.XXXXXX.XXXXXX..R;
    import XXXX.XXXXXX.XXXXXX..R.id;
    import XXXX.XXXXXX.XXXXXX.R.layout;
    
    
    public class FBFragment extends Fragment {
    
    	private static final String target_url = "https://XXXXXXXXXX.com/";
    	private static WebView webView;
    	final Context context = getActivity();
    	View rootView;
    
    
    
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    
    		rootView = inflater.inflate(R.layout.XXXXX_main, container, false);
    
    		
    
    		webView = (WebView) rootView.findViewById(R.id.webview);
    		webView.getSettings().setJavaScriptEnabled(true);
    		
    		...........CONT...
    		
    	
    		
    		
    		
    		
    		
    		
    		
    		
    		.......CODE ENDS WITH..........
    		
    			}
    		});
    		CookieSyncManager.createInstance(getActivity());
    		CookieManager cookieManager = CookieManager.getInstance();
    		cookieManager.setAcceptCookie(true);
    		CookieSyncManager.getInstance().startSync();
    
    		webView.loadUrl(target_url);
    
    		return rootView;
    	}
    
    	@JavascriptInterface
    	public void processVideo(final String vidData, final String vidID) {
    
    
    
    
    	}
    
    
    
    	public static boolean Back() {
    		if (webView.canGoBack()) {
    			webView.goBack();
    			return true;
    		} else
    			return false;
    	}
    }

以下是我的主要活动

		@Override
		public void onBackPressed() {
	

		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("Exit").setMessage("Are you sure you want to exit?").setCancelable(false)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();
					}
				}).setNegativeButton("No", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int id) {
					}
				});

		AlertDialog alert = builder.create();
		alert.setCancelable(false);
		alert.show();

	}

}

2 个答案:

答案 0 :(得分:0)

尝试以下解决方案

在您的活动类的onBackPressed方法中调用片段的Back()方法。

@Override
public void onBackPressed()
{
    yourFragment.Back();
}

在你的片段的Back()方法中调用getActivity()。onBackPressed();像这样

    public static void Back() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else{
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Exit").setMessage("Are you sure you want to exit?").setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    getActivity().finish();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                }
            });

           AlertDialog alert = builder.create();
           alert.setCancelable(false);
           alert.show();
        }
    }

答案 1 :(得分:0)

覆盖您的活动的onBackPressed()

@Override
public void onBackPressed()
{ 
 //fbFragment is an instance of your FBFragment
 WebView webView = fbFragment.getWebView();

 if (webView.canGoBack()) 
        {
            webView.goBack();

        } else{
          super.onBackPressed();

}

}

///这是你的片段里面。请注意getWebview方法

package XXXX.XXXXXXXXXX.XXXXXXXXXXXX.com;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import XXXX.XXXXXX.XXXXXX..R;
import XXXX.XXXXXX.XXXXXX..R.id;
import XXXX.XXXXXX.XXXXXX.R.layout;


public class FBFragment extends Fragment {

    private static final String target_url = "https://XXXXXXXXXX.com/";
    private static WebView webView;
    final Context context = getActivity();
    View rootView;



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

        rootView = inflater.inflate(R.layout.XXXXX_main, container, false);



        webView = (WebView) rootView.findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        ...........CONT...










        .......CODE ENDS WITH..........

            }
        });
        CookieSyncManager.createInstance(getActivity());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        CookieSyncManager.getInstance().startSync();

        webView.loadUrl(target_url);

        return rootView;
    }

    @JavascriptInterface
    public void processVideo(final String vidData, final String vidID) {




    }
public void getWebView(){
  return webview;  
 }