In the event that an Activity has been destroyed, but an AsyncTask still has a reference to the object, is there any way to directly query the referenced (destroyed) activity to determine if the Activity is still valid?
In the event that an Activity has been destroyed, but an AsyncTask still has
Share
In the
onDestroy()method of the Activity, set a flag to indicate that the Activity was destroyed.Additionally, in the
AsyncTaskadd aWeakReferenceto theActivityinstead of a normal reference.In the
onPostExecute()method of theAsyncTask, the Activity is still alive if theWeakReferencestill has a value, and theActivityitself still has the flag set to false.