Can I determine if the link to the action is valid?

In case the Activity was destroyed, but AsyncTask still has an object reference, is there any way to directly request a reference (destroyed) activity to determine if the Activity remains active?

+4
source share
2 answers

In the onDestroy() Method of Activity, set the flag to indicate that the activity has been destroyed.

Also, in AsyncTask add WeakReference to Activity instead of the regular link.

In the onPostExecute() AsyncTask activity is still alive if WeakReference still matters and the Activity itself has a flag set to false.

+3
source

Try to keep WeakReference in your activity.

 WeakReference<Activity> ref = new WeakReference<Activity>(activity); … … … Activity activity = ref.get(); if (activity != null) { // DO SOMETHING } 
0
source

All Articles