I have an application where I have an error dialog pop-up. If i do not touch the screen letting the screen dim off, and then turn the screen on,the pop up dialog is no more there. I want the dialog to be there even if the backlight is turned after it goes off, like in other normal android dialogs.
This is how I am creating the dialog
Dialog lVoiceDialog=new AlertDialog.Builder(this).setIcon(
R.drawable.ic_dialog_alert).setMessage(mVoiceCallMessage)
.setPositiveButton(R.string.yes, clickListener)
.setNegativeButton(R.string.no,clickListener1).create();
lVoiceDialog.setOnDismissListener(dismissListener);
where clickListener and clickListener1 are individual listeners for the positive and negative buttons respectively and dismissListener is listener for running code when the dialog is about to be dismissed.
Please
The other answers make me think I misunderstood the question but just in case..
Your activity is (possibly) being closed when the screen goes off. and restarted when it returns in the normal android activity lifecycle.
To retain your dialog, you would need to save its state when the activity closes and reshow it when it returns.
You can demonstrate this by showing the dialog and rotating the phone. Switching orientation does pretty much the same thing.
use AsyncTask
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
example.dismiss();
}
Why do you need dismissListener?In how many ways your dialog can be closed?if only when user selects 'ok' or cancel then you don't need dismiss listener.
if you have to do some task before the dialog closing then you can do it in ok or cancel listener .
Also, if you for any reason happen to call finish() in onPause() method, getting rid of it can solve the issue.
Related
I have asynctask running, when finish, I dismiss the progressDialog and call AlertDialog accordingly:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
Standy on app
Standby on app
Asynctask on going
Asynctask is finished, progressDialog is dismissed
(Still standby on app) AlertDialog appears as it should be
Home screen
Asynctask on going
Go to home screen
Asynctask is finished, progressDialog is dismissed
AlertDialog is called to show
Goes back to app, but alertdialog doesn't appear, it gives me greyed screen instead.
Is there any way to solve this? I tried to search it but couldn't find someone has same issue..
Ok. (As from your comments).
You can not have your dialog automatically appear while your app is in the background. The dialog is displayed within the context of your Activity(along with its window manager), if the activity is not displayed, nor is your dialog.
To display content while your activity is in the background you have a few options, non of which will be straight forward though.
Attach views directly to Android's WindowManager. This will not work with a dialog, you would have to construct a dialog layout and manually attach it, it would remain foreground above everything else.
Launch your dialog from a transparent activity. Problem here is if you are say on your homescreen then the transparent activity/dialog launches and you then call you application the transparent activity/dialog will be dismissed into the background.
Launch an activity with a dialog theme. This "may" work as needed, if you call your main application keeping the stack correct. Would have to test to be sure.
According to the Android Design Guide you should show a Notification instead of the AlertDialog when your AsyncTask has finished and the Activity is in background (paused). To decide what you should show, maintain a boolean variable in your Activity. Set the variable to true inActivity.onResume() and false in Activity.onPaused().
Basically you implement onPostExecute in your AsyncTask. In there you make the ProgressBar invisible and show the Alert Dialog with the code you show in your question.
More about AsyncTask is here
More about Notifications is here
The Design Guide in question is here. Scroll down to 'Sending Notifications to the User'. Essentially, what you do with the AsyncTask in background is like running a Service. At least from the user experience standpoint. Differences between your approach and a Service are internal, e.g. Android will kill your process when it needs space differently.
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.
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()
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
}
I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.