Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

The below code worked for me

public void onBackPressed (){

    if (webview.isFocused() && webview.canGoBack()) {
            webview.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}

Solution 2:

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();
            }
            returntrue;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Solution 3:

try the following snippet

publicvoidback() {
    if (history.size() > 0) {
        history.remove(history.size() - 1);
        if (history.size() <= 0) {
            finish();
        } else {
            setContentView(history.get(history.size() - 1));
        }
    } else {
        finish();
    }
}

@Override
publicvoidonBackPressed() {
    TopNewsGroup.group.back();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        TopNewsGroup.group.back();
        returntrue;
    }
    return super.onKeyDown(keyCode, event);
}

Solution 4:

This is the code that you need.

publicvoidshowAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle(R.string.alerta);
    adb.setMessage(getResources().getString(R.string.mserror));
    adb.setIcon((status) ? R.drawable.close : R.drawable.alerta);

    adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
       publicvoidonClick(DialogInterface dialog, int which) {
        finish();
    } });

    adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
        } });

    adb.show();
}

And then

publicvoidonBackPressed(){

    if (webView.isFocused() && webView.canGoBack()) {
        webView.goBack();       
    }
    else {
        showAlertDialog(Web.this, "","", false);
    }
}

Post a Comment for "Android Webview With Back Button, If Else"