处理webView中的后退按钮

时间:2015-10-25 04:48:14

标签: android webview

我有一个android项目,我想在使用webview时处理片段中的后退按钮。当我使用webView并点击多个链接然后我点击后面。它关闭了应用程序。我怎样才能进入后页。到目前为止,我已经完成了以下工作:

public class NewsFragment extends Fragment {


private ProgressDialog progressDialog;
private WebView myWebView ;

public NewsFragment()
{

    // Required empty public constructor
}






@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_news, container, false);
    myWebView = (WebView) rootView.findViewById(R.id.mwl_Website);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.requestFocusFromTouch();
    myWebView.setVerticalScrollBarEnabled(true);
    myWebView.setHorizontalScrollBarEnabled(true);
    myWebView.setVerticalScrollBarEnabled(true);
    myWebView.setHorizontalScrollBarEnabled(true);
    myWebView.requestFocusFromTouch();
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(getResources().getString(R.string.WEBSITE));

    new LoadViewTask().execute();

    rootView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
                if ((keyCode == android.view.KeyEvent.KEYCODE_BACK)) {
                    if(myWebView!=null)
                    {
                        if(myWebView.canGoBack())
                        {
                            myWebView.goBack();

                        }
                    }
                }
            }
            return true;
        }
    });


    return rootView;

}
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //Before running code in separate thread
    @Override
    protected void onPreExecute()
    {
        progressDialog = ProgressDialog.show(getActivity(),"Loading...",
                "Loading please wait...", false, false);
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params)
    {
        /* This is just a code that delays the thread execution 4 times,
         * during 850 milliseconds and updates the current progress. This
         * is where the code that is going to be executed on a background
         * thread must be placed.
         */
        try
        {
            //Get the current thread's token
            synchronized (this)
            {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4)
                {
                    //Wait 850 milliseconds
                    this.wait(1000);
                    //Increment the counter
                    counter++;
                    //Set the current progress.
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values)
    {
        //set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result)
    {
        //close the progress dialog
        progressDialog.dismiss();
    }
}

1 个答案:

答案 0 :(得分:2)

您遇到的问题是您的onBackPressed()被调用,您需要覆盖它 -

像这样 -

@Override
public void onBackPressed() {
   if(myWebView!=null) {
       if (webView.canGoBack()) {
           webView.goBack();   
       }
   }
   super.onBackPressed();
}

Ref

相关问题