Disabling the back button with an alertbox? - android

I have an alert box for a log in and when ever the back button is pressed the alert box disappears. I've tried #Override
public void onBackPressed() {
// do nothing.
} but it still just stops the back on the activity not the alertbox. Is there something special i have to do to disable the back button on the alertbox?

I believe setCancelable() is intended for this...
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCancelable(boolean)

Related

Create go back button function like Angry Birds

I'm creating an app in which Go Back Button function like Angry Birds have, I've created popup window, it has close button and other buttons working.
To go back I use:
#Override
public void onBackPressed() {
// Write your code here
initiatePopupWindow();
super.onBackPressed();
}
I know about finish() code that do not go back to pervious Activity
this code is working when goback Button is pressed. Popup Window opens but instantly it go back to previous Activity.
My Question is : How can stay popup window when go back button is pressed and do not leave to previous activity like Angry Bird games has
When user pressed go back button it open the popup window and user have to select one option
i want to make something like that
looking for suggestions
thanks
Remove this line:
super.onBackPressed();
From:
#Override
public void onBackPressed() {
// Write your code here
initiatePopupWindow();
super.onBackPressed();
}

Disable SoftInput Keyboard from Closing OnBackPressed

Just wondering if it's possible to prevent the keyboard from closing when the back button is pressed.
AKA, jump to the previous activity on one tap of the back button.
You can override onBackPressed() so that if the keyboard is showing you just call finish() on your Activity:
#Override
public void onBackPressed()
{
boolean keyboardIsShowing = // determine if keyboard is showing somehow.
if (keyboardIsShowing )
{
finish();
}
else
{
super.onBackPressed();
}
}
I am not sure an exact way to know if a keyboard is showing, but this link can point you in the right way:
How to check visibility of software keyboard in Android?
On a side note, users probably don't expect the Activity to close when the back button is pressed, they probably expect the keyboard to close. I would carefully consider your use case before implementing something like this.

How to listen to onKeydown when alert dialog box is open

I have a check box, upon unchecking the box, I show alert dialog. I want to retain the preference when back key is pressed on display of alert dialog pop up.
I am aware of onKeyDown method and facing problem when I want to use it with alertDialogBuilder. How do I listen to back key/on key down event when there is alert dialog pop up , and retain the checkbox preference when user unchecked>got the pop up>but pressed back key.
Thanks.
I found the answer, there is no need to use onKeydown instead I can use:setOnCancelListener
new AlertDialog.Builder(mContext).setIcon(
android.R.drawable.ic_dialog_info).setTitle(
R.string.pref_title_mms_notification_led_color).setView(v)
.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
mDialogShowing = false;
mNotificationManager.cancel(0);
}
}).setNegativeButton(android.R.string.cancel,

android control back button while a dialog is on the screen

I have a timer which runs continously. When I press the BACK button I made a dialog to appear where you can quit from that intent or go back and cointinue the timer what has been stopped by the BACK button. Well if I click on the contimnue, the onResume() method makes the timer continue and it works good. But, if I press the back button when the dialog is on the screen I want the timer to go on just like if I press the Continue on the dialog. But instead, I press the back button and nothing happens, the timer is stopped and it is not good for me since some of my methods only works if the timer is going or it is stopped by the dialog. But if there is no dialog and the timer is stopped numerous potential errors can happen. So how can I stop the user to press the back button when the dialog is on the screen?
I tried something like this:
if ((keycode==back) && a=0 ) {... a=1 , onPuase()} // dialog comes in onPause() just happened
else ((keycode==back) && a=1 ) {... a=0, onResume()} //I want onResume() to happen here
But it is not good. The dialog appears on the first Back button then it disappears on the second Back (nothing happens here). The timer is still stopped here however the third back button starts the timer. So there is an unecessary Back which can cause troubles since the useres wont know that they have to press it again...
A few advices:
Do not call onResume/onPause manually, only system should make it. Else you'll have unexplainable issues on various devices.
You really want to use OnDismissListener ( http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html ). As starting from ICS, dialog can be dismissed not only by pressing Back key, but also by tapping somewhere on screen, outside the dialog.
If you want to prevent dismissing the dialog by "back" and "tapping out of dialog" - use setCancellable(false) http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean) for the dialog.
Good luck
If you want to be notified when user pressed BACK while your dialog was displayed, use OnDismissListener
implement OnDismissListener in your DialogClass
and override OnDismiss method
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//you can control back button from here
}

How do I return to a calling activity if the user presses BACK?

My MAIN activity is spawning a child activity that contains a ListView. While this ListView is being populated (through an AsyncTask), an indeterminate progress bar is shown.
However, assuming that I am an impatient user and I press the BACK button, the progress bar is cancelled but I am left with a blank screen. I have to press BACK one more time to go back to the MAIN activity.
I would like the app to go back directly to the MAIN activity by pressing BACK only once. Can somebody point me in the right direction? I am thinking I should call finish() somewhere but I don't know where to put it. Thanks in advance!
Use setOnCancelListener() on Dialog (which ProgressDialog extends).
you should override "onBackPressed()"
it will call when the activity has detected the user's press of the back key.
#override
public void onBackPressed(){
this.startActivity(this,MainActivity.class);
}
this code will call the MainActivity when emulator/ipphone back button pressed...
you can try like this,place this code in your activities oncreate block.
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});

Categories

Resources