如何修复On resume方法不会在Webview中被调用为Fragment导航抽屉中的片段

时间:2016-08-15 12:25:40

标签: android webview

我有一个webview作为片段,它在我的片段导航抽屉中扩展片段类。我想检测这个片段的背压。 下面是用于检测片段

上的背压的代码
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){
                    // handle back button

                 getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container,new MyFragment()).commit();
                    return true;
                }
                return false;
            }
        });

在我的webview中,每次按下按钮

时都不会调用此方法
public class WebViewFragment extends Fragment {
    static View view;
    private WebView webView;
    private String url;
    static int count = 0;
    LinearLayout llayout;
    MyProgressDialog progressDialog;


    public WebViewFragment() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }

        try {
            ((AdvancedActivity) getActivity()).getSupportActionBar().hide();
            view = inflater.inflate(R.layout.activity_play_lesson, container, false);
            //Get webview
            webView = (WebView) view.findViewById(R.id.web_view_play_lesson);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                webView.setWebContentsDebuggingEnabled(true);
            }
            llayout = (LinearLayout) view.findViewById(R.id.linearLayout);







            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            url = getResources().getString(R.string.myurl);

            url = "http://myurl//88";
            System.out.println("url for myplay is "+url);
            startWebView(url);

        } catch (InflateException e) {

        }
        return view;
    }





    @SuppressLint("JavascriptInterface")
    private void startWebView(String url) {
        //Create new webviejijnw Client to show progress dialog
        //When opening a url or click on link
        webView.setWebViewClient(new WebViewClient() {
            /*
                        IstarProgressDialog progressDialog;
            */
            //If you do not use this method url links are opeen in new brower not in webview
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (progressDialog == null) {
                    // in standard case YourActivity.this
                    progressDialog = new IstarProgressDialog(getContext());
                    progressDialog.setMessage("Loading... Please Wait...");
                    progressDialog.setCancelable(false);
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.show();
                }
            }

            //Show loader on url load
            public void onLoadResource(WebView view, String url) {

            }

            public void onPageFinished(WebView view, String url) {

            }


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,new ErrorFragment()).commit();
            }

        });
        // Javascript inabled on webview
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        webView.getSettings().setGeolocationEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });
        webView.loadUrl(url);
    }




    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the state of the WebView
        webView.saveState(outState);
    }




    @Override
    public void onResume() {
        super.onResume();


       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){
                    // handle back button


                    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,new MyFragment()).commit();
                    return true;
                }
                return false;
            }
        });
        wakeLock.acquire();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.d("Webview ", "The onStop() event");

    }


}

0 个答案:

没有答案