11

I am working on a WebView android app. I am unable to fix an 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.

ashu
  • 1,756
  • 4
  • 22
  • 41

3 Answers3

29

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.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • I am getting problems while inserting exit dialogue code, can help me in making an exit dialogue here? – ashu Jun 05 '13 at 17:14
  • Sure - open a new question with your specifc problem, what you have tried, where it's going wrong, and I'll help if I can. :) – Ken Wolf Jun 05 '13 at 17:16
1

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

Mohanakrrishna
  • 148
  • 3
  • 13
1

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;
        }
    }
Cevin Ways
  • 984
  • 11
  • 13