Back Navigation in a Webview Android App

2020-03-13 04:40发布

I am working on a webview android app. I am unable fix a issue in my app to back navigate. I am using this code and tried all modifications can be made to this.

 public class DeviceActivity extends Activity
 {
 private WebView web;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    web = (WebView) findViewById(R.id.webView);
    web.getSettings().setJavaScriptEnabled(true); 
    web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    web.getSettings().setPluginsEnabled(false);
    web.getSettings().setSupportMultipleWindows(false);
    web.getSettings().setSupportZoom(false);
    web.setVerticalScrollBarEnabled(false);
    web.setHorizontalScrollBarEnabled(false);

    //Our application's main page will be loaded
    web.loadUrl("file:///android_asset/fbp/index.html");

    web.setWebViewClient(new WebViewClient() {
            @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
            public void onBackPressed (){

    if (web.canGoBack()) {
            web.goBack();       
    }
    else {
        //My exit alert code goes here.    

    }
 }
        });

}

 }

This doesn't navigate back but instead it exits the app. Any help would be appreciated.

3条回答
放我归山
2楼-- · 2020-03-13 05:08

We can use webview method to navigate forward and backward. method boolean canGoForward() for Gets whether this WebView has a forward history item. canGoBack() for Gets whether this WebView has a back history item. goBack() Goes back in the history of this WebView and goForward() Goes forward in the history of this WebView. this is my implementation :

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forward_btn:
                if (mWebView.canGoForward()){
                    mWebView.goForward();
                }else{
                    Toast.makeText(getApplicationContext(), "Tidak ada halaman lagi!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.backward_btn:
                if (mWebView.canGoBack()){
                    mWebView.goBack();
                }else{
                    Toast.makeText(getApplicationContext(), "Tidak bisa kembali!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.reload_btn:
                layoutLoading.setVisibility(View.VISIBLE);
                mWebView.reload();
                break;
            case R.id.back_btn:
                onBackPressed();
                break;
        }
    }
查看更多
再贱就再见
3楼-- · 2020-03-13 05:14

Add this in your Activity code (not in onCreate() or nested anywhere else)

@Override
public void onBackPressed() {
    if (web.copyBackForwardList().getCurrentIndex() > 0) {
        web.goBack();
    }
    else {
        // Your exit alert code, or alternatively line below to finish
        super.onBackPressed(); // finishes activity
    }
}

This will navigate back through the WebView history stack until it is empty and then perform the default action (in this case it finishes the activity).

You should probably also set your WebViewClient to have shouldOverrideUrlLoading return true or you won't load any links.

查看更多
Juvenile、少年°
4楼-- · 2020-03-13 05:19

In your code you have kept the following block in onCreate() method.

if (web.canGoBack()) {
        web.goBack();       
}
else {
    //My exit alert code goes here.    

}

Instead keep the same code in onBackPressed() like this

@Override
public void onBackPressed() {
    if(web.canGoBack()) {
        web.goBack();
    } else {
        super.onBackPressed();
    }
}

also refer to Getting Started: WebView-based Applications for Web Developers in Android

查看更多
登录 后发表回答