简体   繁体   English

控制Android Phonegap中的后退按钮

[英]control back button in android phonegap

hi we are working on android tablet by using phone-gap ,now i have to work with back button while controlling back button if we click on back button it has to alert to log out app for that we are using this code 您好,我们正在通过使用phone-gap在android平板电脑上工作,现在我必须在控制后退按钮的同时使用后退按钮,如果我们单击后退按钮,则必须警告退出应用程序,因为我们正在使用此代码

 public class MainActivity extends DroidGap {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setIntegerProperty("loadUrlTimeoutValue", 120000);
    super.loadUrl("file:///android_asset/www/Common/login.html");   
}

     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
             AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
             alertbox.setIcon(R.drawable.ic_launcher);
             alertbox.setTitle("Are you sure..! You want to exit?");
             alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) {                       
                     finish();
                 }
             });

             alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) {

                 }
             });

             alertbox.show();
         }
         return super.onKeyDown(keyCode, event);
     }
 }
         return super.onKeyDown(keyCode, event);
     }

the issue is if we click on back button it is showing alert for yes or no but before choosing the option yes or no it is navigating to back page we are before we have to stop there until we select yes or no can u suggest me any changes. 问题是,如果我们单击“后退”按钮,则会显示“是”或“否”警报,但在选择“是”或“否”之前,它正在导航至后页,我们必须在此停止,直到选择“是”或“否”,您可以建议我吗?变化。

You can control the back button from PhoneGap's Events API 您可以通过PhoneGap的Events API控制后退按钮

http://docs.phonegap.com/en/2.8.0/cordova_events_events.md.html#backbutton http://docs.phonegap.com/en/2.8.0/cordova_events_events.md.html#backbutton

I don't believe you need to go through native code for that, just listen the event as shown 我不认为您需要为此处理本机代码,只需监听事件,如图所示

in the link above and do whatever you need in the callback. 在上面的链接中,然后在回调中执行所需的任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM