Android Dialog Memory Confusion - android

I am wondering if a Dialog is created and then is dismissed or cancelled by calling dialog.dismiss(); or dialog.cancel();
Does it stay in the memory? Or is it removed from the memory?

http://developer.android.com/reference/android/app/Dialog.html#cancel()
Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered).

Related

android AlertDialog dismiss is not called

I've got some problem..
Well, I create AlertDialog using AlertDialog.Builder, than I using setContentView for setting OK and Cancel buttons and hang up some actions on the dismiss() method. BUT..when I touching anything else this buttons, dismiss() is not called.
So, my question is, Which methods are invokes when i touching and how can i call my methods in this case(touching anything else)?
Thns!
try to set onCancelListener() on the dialog.

When should i dismiss dialogs?

I've done some reading about dialogs in Android and I have an open issue:
When I'm using the Activity's methods that handle the dialogs (such as: onCreateDialog(...)), should I or shouldn't I dismiss the dialog in the onPause()?
Or maybe I should dismiss it only if I retained it - made an Activity member variable that has a reference to this dialog?
I've found this answer: https://stackoverflow.com/a/2851833/501560 saying that I need to explicitly call the dismiss() method, but I've read some other resources saying that the Activity should handle it by itself...
Thanks.
Dismissing a Dialog
When you're ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.
If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.
Using dismiss listeners
If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.
First define the DialogInterface.OnDismissListener interface. This interface has just one method, onDismiss(DialogInterface), which will be called when the dialog is dismissed. Then simply pass your OnDismissListener implementation to
setOnDismissListener().
However, note that dialogs can also be "cancelled." This is a special case that indicates the dialog was explicitly cancelled by the user. This will occur if the user presses the "back" button to close the dialog, or if the dialog explicitly calls cancel() (perhaps from a "Cancel" button in the dialog). When a dialog is cancelled, the OnDismissListener will still be notified, but if you'd like to be informed that the dialog was explicitly cancelled (and not dismissed normally), then you should register an
DialogInterface.OnCancelListener with setOnCancelListener().
You never have to dismiss the dialog if it's managed by the Activity.
The Activity will dismiss the dialog when it's destroyed. If the Activity is pause, Dialog doesn't have to be dismissed.
I thought dialogs are supposed to be dismissed in onStop() or onPause() in the Activity lifecycle.
https://developer.android.com/reference/android/app/Dialog.html#dismiss()

How to preload a dialog

I have a custom dialog and despite putting in all the code to inflate the view in its constructor, it seems like it takes much longer when the dialog is first launched compared to subsequent launches, as if only when i call dialog.show(); it actually creates it. How could I do what I intended, to properly preload the dialog to prevent this first run delay?
You can Extend your class with AsyncTask<> and perform all work in doInBackground().in onPreExecute() Show Dialog and in onPostExecute() remove the dialog from UI.

Prevent onCreateDialog being called when process is killed then relaunched

I have an Activity with ProgressDialog being updated by an AsyncTask. I am using onCreateDialog to setup the dialog. The AsyncTask is writing to the SDCard. During normal scenarious (rotation, going to background, etc.) I have no issues.
The problem is that the dialog gets recreated if the process gets killed. Thus, I end up with a "newly" opened activity and a dialog that is not supposed to be shown at all because there is no AsyncTask set up to update it.
For example in case when SD card gets ejecter then the Reaper comes and kills the process (no onDestroy, noPause, noResume has been called by the framework). When, however, the application is resumed (for example from the recently used applications) there is no clue that there is no AsyncTask and I am forced to show the dialog. I cannot return null in onCreateDialog, because the app will crash.
How can I prevent a dialog to be recreated after the process is killed?
Example:
- Activity gets shown
- onCreateDialog/onPrepareDialog show a progress dialog
- AsyncTask gets started exporting to SD card
=> SD card gets unmounted
- Process is killed
- User selects the application from task switched
- Activity gets created as new
=> Android calls onCreateDialog/onPrepareDialog with the previously shown dialog ID
By the time the activity is recreated as new there is no AsyncTask, there is even no SD card. Still, Android insist that I show a dialog.
How can I prevent onCreate/PrepareDialog methods to be called during the recreate? Or the only choice is to bring up an error dialog instead.
you can make check over SD card if it is inserted or ejected thru
String state = Environment.getExternalStorageState();
and over it's return value you can control your dialog...
OnCreateDialog method is like onCreate method which will be called only once in your activity. You cannot control your dialog using this method. Instead your activity should also have the following piece of code which will be called each time Dialog is shown. So you have to handle things inside this method instead.
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
}

AlertDialog.show silently ignored within a service

I have a service running a background thread. What I'd like to do is to
show an AlertDialog initiated from my background thread. I know that
this is not the recommended way of notifying the user and that it
interrupts the workflow (as they can pop-up in any application at any
time) but it's a suitable way for my use case.
There is a handler registered with the background thread and showing a
Toast notification withing the handler works fine. But after switching
to an AlertDialog nothing happens anymore. My showDialog logic is
silently ignored. No Dialog window appears, no log entry. It's a bit
strange as I'd expect at least a log entry saying that I'm doing
something wrong or whatever.
Are there any limitations for showing an AlertDialog initiated from a
service background thread? Some people seem to recommend a Dialog themed
Activity to get a similar behavior.
Any clarification or help making it work is greatly appreciated!
Yves
It is possible to open a dialog from a background thread. The trick is to start an activity which looks like a dialog:
<activity android:label="#string/app_name" android:name="YourDialog" android:theme="#android:style/Theme.Dialog"/>
Then, to start your activity:
Intent dialog = new Intent(this, YourDialog.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialog);
Note that this is asyncrhonous and doesn't block. If you want to process the result, you'll have to use startService() and pass a custom activity to indicate a result.
Emmanuel
Are there any limitations for showing an AlertDialog initiated from a service background thread?
The limitation is: it's not possible, AFAIK. You have to show dialogs from the main application thread, not just some arbitrary thread on which you have a Handler.
Some people seem to recommend a Dialog themed Activity to get a similar behavior.
That would seem to be the most likely solution, AFAICT.
Another trick is to stay with the AlertDialog but with an additional window type TYPE_SYSTEM_ALERT:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Are you sure?")
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
But don't forget to add this permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Categories

Resources