سؤال

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?

هل كانت مفيدة؟

المحلول

You can put it in the onCreate or onActivityCreated method.

More info on the fragment lifecycle: Fragments.

نصائح أخرى

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>);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top