views:

33

answers:

1

I have the following problem:

I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back. At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.

This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.

How can I achieve a dialog that is only shown if the activity is still active?

+1  A: 

Call isFinishing() on your activity.

Jim Blackler
Thanks that seems to work. Are all Activities left with the back button finishing?
Janusz
Despite the odd name I believe it just indicates if the Activity is visible to the user or not.
Jim Blackler
No is returns only true if the activity is going to be destroyed. If I leave an activity forward meaning that I'm able to go back to it via the back button the activity is only paused and not finishing.
Janusz