I have AsyncTask doing background work and populating ListView in its onProgressUpdate and calling notifyDataSetChanged() on adapter. Problem is that when orientation changes, AsyncTask stops. How can I make AsyncTask keep doing its work and populating ListView with results no matter what? I cant use android:configChanges="keyboardHidden|orientation" because my layout is different in landscape mode. Also I tried using Service class, but I was unable to access my UI components. What is the most simple way to achieve what I am after for?
I have AsyncTask doing background work and populating ListView in its onProgressUpdate and calling
Share
You could use a callback method to update your listView. You need to implement it in your AsyncTask and call if directly from your doInBackground or onProgressUpdate:
And then in doInBackGround (or onPogressUpdate):
Then in your activity you can save a reference of your AsyncTask in another class:
This way you can get a reference to your AsyncTask even if the activity is recreated adding something like this on your oncreateView():
And finally you can add this to the updateListView method you will be overriding in your activity:
This is just some code that I think will work for you since You didn’t posted any code but you get the idea that when you rotate your device the AsyncTask can’t update something that has been destroyed like a listView so it’s better to keep the reference to the asyncTask.