Android CordovaActivity在退出之前显示确认对话框 [英] Android CordovaActivity display confirmation dialog before exiting

查看:862
本文介绍了Android CordovaActivity在退出之前显示确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用cordova / html5开发了一个跨平台移动应用程序。 Android应用程序从CordovaActivity扩展的活动开始。我们要在应用程序退出之前显示一个配置对话框,使用设备上的点击按钮。我们编写了以下代码。但活动正在退出,不显示确认对话框。

We have developing a cross platform mobile application using cordova/html5. The android application starts with an activity extended from CordovaActivity. We want to display a conformation dialog before application exiting using taping on the device tap button. We have written the following code. But the activity is exiting without displaying the confirmation dialog. But if the activity is extends from Activity class, then the conformation dialog works fine.

public class MyActivity extends CordovaActivity {

@Override
public void onBackPressed() {
    super.onBackPressed();
    // Display alert message when back button has been pressed
    Log.i("&&&&&&&&", "%%%%%%%%% onBackPressed *************");
    backButtonHandler();
    return;
}

public void backButtonHandler() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            MyPortal.this);
    // Setting Dialog Title
    alertDialog.setTitle("Leave application?");
    // Setting Dialog Message
    alertDialog
            .setMessage("Are you sure you want to leave the application?");
    // Setting Icon to Dialog
    //alertDialog.setIcon(R.drawable.ic_launcher);
    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to invoke NO event
                    dialog.cancel();
                }
            });
    // Showing Alert Message
    alertDialog.show();
}
}


推荐答案

I没有仔细看你的代码,但为什么不在js侧实现这个功能?

I haven't looked at your code in detail but why don't you implement this feature in js side?

您可以在js中注册backbutton事件,执行您的操作,如果需要,调用nav.app.exitApp()。

You can register to "backbutton" event in your js, do your thing and call nav.app.exitApp() if needed.

这篇关于Android CordovaActivity在退出之前显示确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆