Question

I have this little problem.

I fetch data from network into a ListView using AsyncTask. If I press on an item it takes me to another activity. Then, if I press the 'back' button, the AsyncTask does not execute again. (Which is exactly what I want).

The problem is, if on the ListView I press the back button, and then I get into the listView activity again, the AsyncTask executes again.

I'm not sure if this is happening because the Listview's activity destroys and loses all reference, or if because onResume (I think) is recalling onCreate.

Was it helpful?

Solution

When you press back, you go back to the same old instance of the activity.

If you want the asynctask to run again you need to call its execute in onResume.

From what you have told, it seems you call the asynctask execute in the activity oncreate. When you go back from your listview activity and come back , you are creating the activity again and that is why your asynctask is executed. onResume is always called after onCreate. onResume is always called when your activity comes into focus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top