Android Webview with Back Button, if else

£可爱£侵袭症+ 提交于 2019-12-03 03:16:45
Arun

The below code worked for me

public void onBackPressed (){

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

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);
}

try the following snippet

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

@Override
public void onBackPressed() {
    TopNewsGroup.group.back();
}

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

This is the code that you need.

public void showAlertDialog(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() {
       public void onClick(DialogInterface dialog, int which) {
        finish();
    } });

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

    adb.show();
}

And then

public void onBackPressed(){

    if (webView.isFocused() && webView.canGoBack()) {
        webView.goBack();       
    }
    else {
        showAlertDialog(Web.this, "","", false);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!