Reputation: 351
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
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.
Upvotes: 0
Views: 2676
Reputation: 623
You can control the back button from PhoneGap's Events API
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.
Upvotes: 3