Search code examples
androidmultithreadingback-button

How to handle feedback from background threads if the user uses the back button?


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?


Solution

  • Call isFinishing() on your activity.