I have an activity
with in which there is a async task
that will do some download stuff. AT the time of downlaoding it will show a loading dialog
.
My problem is, it worked fine for me when me doing it in only one orentiaon
. But when i rotate
at the time of download, it shows window leaked
and will crash at the
dialog.cancel
in my post excute.
From my study on it more i understood it due the change in the context
when device is rotated
.
That is when a device
is rotated
the activity
will be recreated
so the context
will be changed
.
But i have created the dialog with old one and that wasn't the current context
. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb
, me tried but with fragment
but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
1 Answers
First of all: open your dialog using the showDialog
method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance
to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance
to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask
, you must be aware that after the activity is recreated, the AsyncTask
may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask
progress notifications from the current UI elements.

- 198,401
- 62
- 356
- 264
-
Thanks **Cristian** for you answer. Me used [ProgressDialog](http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog) but for me crashing while rotating. – Jithin Mar 05 '11 at 04:53
-
What is this **Proxy Design pattern**. Its sound interesting. I don't know anything about it. can you provide some links for that – Jithin Mar 05 '11 at 04:54
-
Take a look at the source code of the [iosched](http://code.google.com/p/iosched/) app. It has a nice example of how to use `onRetainNonConfigurationInstance`, `getLastNonConfigurationInstance`, as well as the Proxy Design Pattern. – Cristian Mar 05 '11 at 13:51