Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Webview with Back Button, if else

Disregard. I was opening the wrong app that was installed. It works wonderfully. :)

I have the back button working correctly within my webview, but I was wondering something. How to make the webview go back until it cannot anymore, and instead of exiting the program, have it open up a dialog box asking if the user is sure they won't to exit. Here is my take on the code.
Thanks for taking the time.

.java file

package com.vtd.whatthe;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WhatThe extends Activity {
    private WebView webview;

    /** Called when the activity is first created. */

    public void onBackPressed (){

        if (webview.isFocused() && webview.canGoBack()) {
                webview.goBack();       
        }
        else {
                openMyDialog(null);

        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new HelloWebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setInitialScale(50); 
        webview.getSettings().setUseWideViewPort(true); 
        webview.loadUrl("http://test2.com/");
    }
    private class HelloWebViewClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

            }

    public void openMyDialog(View view) {
        showDialog(10);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 10:
            // Create our AlertDialog
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit? You have unlimited guesses!")
                    .setCancelable(true)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // Ends the activity
                                    WhatThe.this.finish();
                                }
                            })
                    .setNegativeButton("Keep Guessing!",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    Toast.makeText(getApplicationContext(),
                                            "Good Luck!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });

            return builder.create();


        }
        return super.onCreateDialog(id);
    }
}
like image 527
Josh Fairbank Avatar asked Jun 02 '11 21:06

Josh Fairbank


2 Answers

The below code worked for me

public void onBackPressed (){

    if (webview.isFocused() && webview.canGoBack()) {
            webview.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}
like image 195
Arun Avatar answered Sep 18 '22 22:09

Arun


Try this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webView.canGoBack())
            {
                myWebView.goBack();
            }
            else
            {
                finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}
like image 39
Zar E Ahmer Avatar answered Sep 16 '22 22:09

Zar E Ahmer