I have an asynctask in my fragment.Whenever i get in this fragment it loads all the data from the beginning but it's waste for me.So i want that it loads only one time.How can i fix this?
Asked
Active
Viewed 7,139 times
2 Answers
4
You can put it in the onCreate or onActivityCreated method.
More info on the fragment lifecycle: Fragments.

Scott
- 1,263
- 1
- 12
- 23
-
I think the problem is that.I put asynctask execute method in onCreate or onActivityCreated ,the result never changed.It loads every time.I use this fragment in view pager. – droidmachine Feb 20 '12 at 18:56
-
you can try including setRetainInstance(true); in your onActivityCreated method. Doing so will cause onCreate() of the fragment not to be called on each configuration change. – Scott Feb 20 '12 at 19:45
0
Where in your code do you wish to load your initial data? Do you initially load it from parent activity? Wherever that is, that's where you need to implement your AsyncTask and maintain list of items. not within Fragment. From there, you simply need to do the following in your fragment:
public class Android extends Fragment{
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/** Grab your initial data **/
setListAdapter(new ArrayAdapter<String>(context, layout, <your data>);
}
}

LuxuryMaster
- 577
- 1
- 4
- 11
-
where does
come from? how does one get the data from the parent activity? – topwik Aug 07 '12 at 18:16