1

I'm using an app with a navigation drawer menu, and I want all of the fragments to be created when the app starts, as some of the fragments fetch data from a web service and I do not want the user to have to wait for all of the data to load right when they go to a screen. To work around this, I want to create all of the fragments at once and then hide them, and then afterwards display the correct fragment.

  private void initFragments() {
    FragmentManager fManager = getFragmentManager();
    FragmentTransaction fTransaction = fManager.beginTransaction();
    for (int i = 0; i < MAIN_FRAGMENTS; i++) {
        fTransaction.add(R.id.frame_container, fragmentArray[i], fragmentTags[i]);
    }
    fTransaction.commit();
    fManager.executePendingTransactions();
}
private void hideFragments(){
    FragmentManager fManager = getFragmentManager();
    FragmentTransaction fTransaction = fManager.beginTransaction();
    for (int i = 0; i < MAIN_FRAGMENTS; i++) {
        fTransaction.hide(fManager.findFragmentByTag(fragmentTags[i]));
    }
    fTransaction.commit();
    fManager.executePendingTransactions();
}

My way to work around this was to just create all of the fragments and then hide them by calling

initFragments()
hideFragments()

However, I've noticed that when I run this on the emulator, on some devices it will correctly only show one Fragment, with the rest being added but hidden. But on some other devices, it will show all of the fragments overlaid ontop of eachother resulting in an ugly mess. Furthermore, when I use the app on a landscape device such as a tablet, the fragments will also overlap eachother as I have forced the app into a portrait only mode. Any ideas on the best way to go about this?

Robert Sun
  • 43
  • 1
  • 5
  • 1
    As per your implementation I believe your fetching of data from web service doesn't need any user inputs, you are fetching it once and do the display. You can fetch all the data you need in splash activity or somewhere after that , and store that in a container class and then display everything smoothly. Rather than doing frequent fragments transitions. – theJango May 14 '15 at 14:21

1 Answers1

1

Are you getting any errors?

I suspect some devices/versions might not support the findFragmentByTag() method, or the hide() method for that matter. Try looking into that.

With regard to your overlapping problem on tablets, try this: https://stackoverflow.com/a/11086185/2929693

Community
  • 1
  • 1
D. Visser
  • 875
  • 2
  • 12
  • 19
  • There don't seem to be any errors that occur. And furthermore, when I launch it using a phone emulator, the overlapping fragments only occurs when the emulator first starts. After closing the app and reopening it, the overlapping fragments never occurs – Robert Sun May 14 '15 at 14:23
  • That's probably because you're calling this from the `onCreate()` method, aren't you? Try calling `initFragments()` **and/or** `hideFragments()` from the `onResume()` method. If you're curious about the different states of an Activity, refer to the [official documentation](http://developer.android.com/training/basics/activity-lifecycle/starting.html). – D. Visser May 14 '15 at 14:25
  • I indeed called it from `onCreate()` before, but I just tried calling both of them in `onResume()` just now and it still gives me the same result. On **some** devices, it will start out with overlapping fragments but never again after the app restarts. – Robert Sun May 14 '15 at 14:33
  • [This way](http://stackoverflow.com/a/18274767/2929693) of accomplishing the same seems a little hacky, but does the trick. You could try that until someone comes along with a better answer. – D. Visser May 14 '15 at 14:36
  • By the way, are you sure that you are iterating through the for-loop, on all devices? And are all the devices you tested API level 11 or above? It sounds silly, but simple things like that cause the biggest headaches sometimes. – D. Visser May 14 '15 at 14:42
  • It seems like I was able to fix the overlapping fragments on start up by changing the initFragments method by checking to see if the fragment manager could find a fragment by a certain tag, and if the fragment was null, add the fragment. I believe at start up it would call initFragments and hideFragments twice for some reason? Not sure why – Robert Sun May 14 '15 at 14:51