Your correct AsyncTask address does not know the new instance of the activity, because the activity was destroyed and recreated during the orientation change.
But there is a method that is called when the orientation changes and which you can use to pass AsyncTask to the new action.
onRetainNonConfigurationInstance () is called before the action is destroyed, override the method and return the link to your running AsyncTask. Inside the onCreate () method, you can get AsyncTask due to the call to getLastNonConfigurationInstance () . Keep in mind that you should handle cases when your activity is created for the first time, and getLastNonConfigurationInstance () returns null.
In addition, you must transfer your activity to AsyncTask so that it can reference the progress bar of the current activity. Therefore, I propose implementing two methods for registering and unregistering activity to / from AsyncTask. Thus, in onRetainNonConfigurationInstance () you unregister the βoldβ activity that will be destroyed, and in onCreate you either register the newly created activity with the new AsyncTask, or the one you retrieve from getLastNonConfigurationInstance ().
source share