Search code examples
javaandroidandroid-fragments

How to increase Loadspeed of Fragments with the fragment manager in Java while developing an android application?


I'm pretty new to android development. My program consists of a single activity and a bunch of fragments which can be navigated to with a menu.

Some Fragments only consist of ~12 TextViews and load fast enough others however are taking a long time. The Fragments contain 8-12 Numberpicker & 8-12 EditTexts these contain values, which have been saved with the sharedPreferences. The elements in the fragment are saved to members in the onViewCreated method and then filled with the data from the sharedPreferences. I'm currently selecting the Fragments in the following way:

} else if (itemId == R.id.nav_2weightlifting) { getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ex_two).commitNow();

With "nav_2weightlifting" being the menu entry and "ex_two" being an on startup created Fragment which corresponds to the menu entry.

It seems like the SupportFragmentManager creates and destroys the fragment every time a menu is switched. This takes between 1 & 2 seconds and is very clunky.

I've tried to create the Fragments before giving them to the FragmentSupportManager. I've tried to load all data from the SharedPreferences together and use the resulting map.

Is there a way to create every fragment and only show the selected one without deleting everything else? Or is there another way for my information to be loaded faster (maybe not in the onViewCreated())? Are Fragments the wrong tool to achieve what I want?

Any help is greatly appreciated


Solution

  • try these things :

    1. Use Fragment Caching
    2. Use show() and hide() Instead of replace():
    3. Try Asynchronous Loading with AsyncTask - Sharepref