我们使用cordova / html5开发跨平台移动应用程序。 android应用程序以CordovaActivity扩展的活动开始。我们希望在应用程序退出之前使用设备点按按钮显示一个确认对话框。我们编写了以下代码。但是活动正在退出而不显示确认对话框。但是如果活动是从Activity类扩展的,那么conformation对话框就可以正常工作。
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();
}
}
答案 0 :(得分:1)
我没有详细查看您的代码,但为什么不在js端实现此功能?
您可以注册到" backbutton"在您的js中执行事件,执行您的操作并根据需要调用nav.app.exitApp()。
答案 1 :(得分:0)
这里的最佳做法是在javascript端处理所有这些内容,但是如果你仍想在cordova插件中实现相同的内容,请更改行
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MyPortal.this);
到
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
this.cordova.getActivity());
它会起作用。
我知道我的答案为时已晚,但可能会帮助仍有需要的人。