views:

68

answers:

2

I have read the other window leak posts and have tried what what suggested there to no avail.

I have 3 activities: A, B, and C. Activity A gathers information from the user. Activity B displays a spinning ProgressDialog while it communicates with a server on another thread. When the thread finishes, it dismisses the ProgressDialog and starts the next activity. Activity C displays the information from the server to the user. Activity B is set up so that when the user hits back from C, they fall back to A.

It is important that these tasks be in separate Activities.

As of now the app successfully does what it is supposed to in most cases, except in the following scenario: If the user changes the orientation while in activity C before returning to Activity A, the app crashes due to a window leak.

  • I am dismissing the ProgressDialog inthe onPause() of Activity B before istart C.
  • I have tried dismissing the ProgressDialog on the main thread using a handler as well as in the separate thread.
  • When the user does not change the orientation in C, no window leak occurs.

Any ideas? TIA

A: 

This often happens where ProgressDialogs are used. I experimented with ProgressDialog a while back and found the thing to do was dismiss() it from onPause() and create it anew from onResume(). The background task obviously needs to survive your Activity & dialog so I used onRetainNonConfigurationInstance() to pass the task instance from the destroyed Activity to the new one.

An alternative, cheatier workaround might be to simply prevent your Activity from being destroyed and created anew merely because the screen orientation changed. Do this by adding android:configChanges="orientation" to the tag(s) in your AndroidManifest.xml.

Reuben Scratton
maybe you didnt read the question above. i am already calling dismiss() in onPause(), so that isn't the problem. I wouldn't want to recreate the dialog in onResume() because when i return to Activity B from Activity C, i immediately close it by calling finish() in onActivityResult(). the user shouldn't see activity B at all when returning from Activity C. they should just fall through to A. The window leak occurs when the app returns to Activity A.
mtmurdock
What do you think happens if the screen is reoriented while Activity B is current and before Activity C is shown?
Reuben Scratton
thats not what I'm asking. The error occurs when the orientation is changed in Activity C. Read the question.
mtmurdock
I've read your question several times... a little source code would go a long way to being able to answer it properly. How exactly are you creating activity C, and how are you finishing activity B? Is your background task keeping any long-term references to UI elements such as the activity and/or progress dialog? In the meantime I was pointing out you have a separate problem which stems from not managing the dialog properly w.r.t. possible lifetimes of B.
Reuben Scratton
A: 

I solved my problem by completely changing how i handled everything. I now have only two activities (A and B) and display the ProgessDialog in activity B while handling the savedInstanceState as needed in order to work around the problem.

Even though I have fixed the problem on my app, I still dont know why it was occuring before and would like to learn more about window leaks and why i was having problems. If anyone knows more about the problem i was having, please post as I'm sure there are others with the same problem.

Thanks

mtmurdock