Fragment+webview实现物理返回键的监听

http://blog.csdn.net/dodod2012/article/details/50476779原作来源

首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法中处理各自对Back事件的消费逻辑。onBackPressed返回布尔值,宿主Activity将会根据该方法的返回值判断子Fragment是否有消费Back事件。此外,宿主FragmentActivity还会保持一份当前Fragment的引用,当用户按下Back键时,宿主Activity会判断当前Fragment是否需要消费该事件,如果没有Fragment消费才会自己消费。


public abstract class BackHandledFragment extends Fragment{  

    protected BackHandledInterface mBackHandledInterface;  

    /** 
     * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑 
     * FragmentActivity捕捉到物理返回键点击事件后会首先询问Fragment是否消费该事件 
     * 如果没有Fragment消息时FragmentActivity自己才会消费该事件 
     */  
    public abstract boolean onBackPressed();  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        if(!(getActivity() instanceof BackHandledInterface)){  
            throw new ClassCastException("Hosting Activity must implement BackHandledInterface");  
        }else{  
            this.mBackHandledInterface = (BackHandledInterface)getActivity();  
        }  
    }  

    @Override  
    public void onStart() {  
        super.onStart();  
        //告诉FragmentActivity,当前Fragment在栈顶  
        mBackHandledInterface.setSelectedFragment(this);  
    }  
}  

宿主FragmentActivity需要实现BackHandledIntegerface,子Fragment会通过该接口告诉宿主FragmentActivity自己是当前屏幕可见的Fragment。

public interface BackHandledInterface {  
    void setSelectedFragment(BackHandledFragment selectedFragment);  
}  

所以在Fragment的onCreate中会判断宿主FragmentActivity是否已继承了该接口。在Fragment的onStart()方法中就会调用该接口告诉宿主FragmentActivity自己是当前屏幕可见的Fragment。
宿主FragmentActivity就可以在onBackPressed()方法中对Back事件进行判断处理了。

public class MainActivity extends FragmentActivity implements BackHandledInterface{  

    private BackHandledFragment mBackHandedFragment;  
    private boolean hadIntercept;  

    @Override  
    public void setSelectedFragment(BackHandledFragment selectedFragment) {  
        this.mBackHandedFragment = selectedFragment;  
    }  

    @Override  
    public void onBackPressed() {  
        if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){  
            if(getSupportFragmentManager().getBackStackEntryCount() == 0){  
                super.onBackPressed(); //退出  
            }else{  
                getSupportFragmentManager().popBackStack(); //fragment 出栈  
            }  
        }  
    }  
}  

Fragment需要继承抽象类BackHandledFragment,并覆写抽象方法onBackPressed(),在抽象方法中判断WebView是否可以返回到之前打开的网页

public class WorkFragment extends BackHandledFragment {  

    private WebView webView;  
    private WebSettings webSettings;  

    private String url = "https://www.baidu.com/";  
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

        View view = inflater.inflate(R.layout.work_fragment,container,false);  
        webView = (WebView)view.findViewById(R.id.workWebView);  

        webSettings = webView.getSettings();  
        webSettings.setJavaScriptEnabled(true);  
        webSettings.setUseWideViewPort(true);  
        webSettings.setLoadWithOverviewMode(true);  

        webView.setWebViewClient(new MyWebViewClient());  
        webView.loadUrl(url);  

        return view;  
    }  

    private class MyWebViewClient extends WebViewClient {  
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {  
            //    return super.shouldOverrideUrlLoading(view, url);  
            view.loadUrl(url);  
            return true;  
        }  

        @Override  
        public void onReceivedError(WebView view, int errorCode,  
                                    String description, String failingUrl) {  
            //     super.onReceivedError(view, errorCode, description, failingUrl);  
            //  Toast.makeText(this,"网页加载错误!",0).show();  
        }  
    }  
    @Override  
    public  boolean onBackPressed(){  

        if(webView.canGoBack()){  
            webView.goBack();  
            Log.v("webView.goBack()", "webView.goBack()");  
            return true;  

        }else{  
            Log.v("Conversatio退出","Conversatio退出");  
            return false;  
        }  

    }  

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值