0

I have facing loading issue with fragment.

Steps:I call one fragment from Activity and in fragment i start loadAsyncTask(); for fetching data from server.It takes time to load data.I think it is slow when fragment load. 1.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.list_fragment,
                container, false);

        init();
        setHasOptionsMenu(true);
        loadAsyncTask();
        return rootView;
    }

Searching in google, i find solution. I use loadAsyncTask() method in onActivityCreated() of fragment instead of in onCreateView() like

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadAsyncTask();
    }

Is it right way? because my ui components initialize in init() method of onCreateView().

Arun Kumar
  • 2,874
  • 1
  • 18
  • 15

1 Answers1

0

If your fragment doesn't rely on the data fetched from the internet, then the AsyncTask won't affect the speed of the fragment creation. AsyncTask runs in a separate thread so it doesn't block the main thread from where onCreateView() is executed. However onCreateView() method should be responsible only for creating and returning the view hierarchy associated with the fragment.

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • Now it become fast when is used loadAsyncTask() in onActivityCreated().My fragment is rely on the data fetched from the internet because i update ui components when data fatched from internet. I follow below link http://stackoverflow.com/questions/9237431/changing-tabs-is-slow-laggy-using-fragments – Arun Kumar Oct 16 '14 at 16:55
  • that's the good solution .As long as you do your heavy network operation in doInBackground() method which is a separate thread and update your UI in the main thread – Marcin S. Oct 16 '14 at 17:51