7

I have an Activity that uses tabs, and the tabs switch Fragments. The problem is that the Fragment take a few seconds to load when being created, thus switching tabs has a delay of about 1 or 2 seconds. To fix this I have been trying to find a way to display a simple Loading graphic or even a progress dialog, so that the tab changes instantly and displays something indicating things are loading until everything completes.

My onCreateView method of the Fragment looks like this:

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

    doHeavyStuff();

    return fl;
 }

I tried putting doHeavyStuff() in onStart() but that did not help anything. And a Thread won't help because doHeavyStuff() involves manipulating views/GUI.

Any ideas on how I can display the Fragment and display "Loading" information while everything else loads?

Thanks!

Matt.

Matt M
  • 814
  • 11
  • 23

2 Answers2

8

In the onActivityCreated() :

  • Start a progress bar(or any progress showing UI element/s).
  • Execute the HeavyStuffDoingAsyncTask which is explained below.

HeavyStuffDoingAsyncTask should have:

  • doHeayStuff()'s logic which doesn't update ui in the doInBackground method.
  • call publishProgress() from doInBackground() everytime you want to update the ui.
  • Implement the UI updating logic in the onProgressUpdate method.
  • Stop the progress bar in the onPostExecute method.

Good Luck.

500865
  • 6,920
  • 7
  • 44
  • 87
  • I'm only inflating view in onCreateView() and that's also laggy. So should I change that inflating code like above for better performance? – Hussain Mansoor Dec 02 '13 at 17:55
  • Are you doing anything other than just inflating the view? like populating the views etc.,? If so, you should do the populating part outside of onCreateView() to avoid the laggy behaviour. – 500865 Dec 02 '13 at 19:44
  • No I'm only inflating the view. return inflater.inflate(R.layout.setting_list_layout, container, false); is the only line in onCreateView(); – Hussain Mansoor Dec 03 '13 at 06:49
  • Post the full code as a new question. You might get some help. I would think that the laggy behaviour is because of something else. – 500865 Dec 03 '13 at 08:20
3

try to set

pager.setOffscreenPageLimit(no_of_fragments or pages);
Thamays
  • 2,978
  • 26
  • 31