Question

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?

Was it helpful?

Solution

You can put it in the onCreate or onActivityCreated method.

More info on the fragment lifecycle: Fragments.

OTHER TIPS

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>);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top