android control back button while a dialog is on the screen - android

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
}

Related

How to show alert Dialog when home button is pressed ?(ICS)

I want to show alert when user presses home button on device(do you want to exit the app ?)
How to do it ?
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
#Override
public void onBackPressed() {
// your dialog here
}
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Android Overriding home key
Write the show alert dialog code in onPause() it will work perfectly.

android: dim the screen when application is in background

I am developing a small app which shows passwords of the user through a Dialog screen.
When home button is pressed, I need to dim the screen (on the multi tasking window) so that any other person cannot see the password.
When user re-opens the app, it asks an application lock. But if the user leaves the password Dialog open and presses the home button, dialog and the password which user last looked at stays visible (on the multi tasking window) for a while (3-4 seconds!!) until a new dialog asks the lock.
So far I tried ever possible dialog.dissmiss() options. Dialog dismisses only when app is opened again (until a new lock dialog appears) even I put dismiss() in onPause, onStop etc.
Any idea appreciated.
I also tried,
android.os.Process.killProcess(android.os.Process.myPid());
this.finish();
System.exit(0);
none of them actually worked.
Suggestion 1: Double-check your implementation. Tying your dialog to the activity lifecycle seems like a good idea (especially to avoid leaked window errors as described here)
The following example works out well for me (with coachMark being derived from Dialog)
#Override
protected void onResume()
{
log.debug("onResume");
super.onResume();
// Show the coachMark depending on saved preference values
coachMark.mayBeShow();
}
#Override
protected void onPause()
{
log.debug("onPause");
// Hide the coachMark if it is showing to avoid leakedWindow errors
coachMark.maybeHide();
super.onPause();
}
onPause definately gets called when you press the home button, so if this approach does not work for you, try not recreating the dialog in the restarting part of the acitivty lifecycle (onRestart(), onStart() and onResume()) and see, if it gets dismissed correctly.
Suggestion 2: Should all of the above fail, you might consider overriding the home button as described here. I highly advise against it though, since this may cause the app to work in an way that the user does not expect it to.

Dismissing dialog on activity finish

In my app I have several activities one after the other. After my login screen I have Home screen and after that several screens. Now When user select device home button or power off button I want to display login screen when user again comes to my app and then Home screen. Rest all activity I am finishing it from my base class. Now till here I have done, My problem is when I show a dialog in some other activity and at that instance if user click on home or power button, then i am getting WINDOW LEAKED EXCEPTION.
Like I have TempActivity is displaying a dialog and user clicked home button so StoreActivity and TempActivity will finish but Dialog never got chance to be dismissed. So What would be the best way to deal with this situation.
Is there some better way to dismiss the dialog so that I don't get any exception.
Override onDestroy, there, check whether the dialog is present, if so, dismiss it.
dismiss() in onDestroy() doesn't solve this problem. Try to override activity.finish() like:
#Override
public void finish() {
if(mDialog != null) {
mDialog.dismiss();
}
super.finish();
}
Put the Dialog handle in a member object, then when you finish the top activities, dismiss the dialog first.
You could make this more neat by creating abstract Activity class (which all your activities extends), which dismisses possible dialog when calling finish()

Which function I have to call on a Cancel button in a user interface (activity)?

I have various activitys on my app. I have a configuration Activity, and I want to put a cancel button, that, when user press this button, the configuration window get's closed and turn back into the previous activity.
I found something about calling cancel or dismiss functions, but I can't call them cause this is not a dialog, it's an activity.
You can finish an activity.

AlertDialog disappears after switching activity

I have an android application having an AlertDialog with OK and Cancel buttons. When the dialog shows without pressing the OK or Cancel button, just press Home button of device. The Home screen will show. Now open another application suppose Camera. Take some picture or Video. Now get out from the Camera application. Now open my android application and surprisingly the alertdialog have disappeared. Why?
I'm guessing you are creating this AlertDialog onCreate() method.
First, you should read up on the Activity Lifecycle.
And what happens is that when you go to another app, the Activity goes to onPause method, which cleans up a bit.
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Then because you return to the app, it calls the onResume method, which doesn't create your dialog again.
If you want to show dialog on startup of application then write this code in
onResume()
method, it will show dialog every time when user returns to this screen.
Or you can manage its state in
onPause()

Categories

Resources