how to avoid onCreateView() when swiping in tabs

I have an application with an activity and 3 swipable tabs(fragments). I have some heavily created UI elements inside tabs which take some time to be created and it’s ok for the first run, but the views are recreated every time I swipe them.

When I select a tab, the other tab calls onStop() and when I select the previous tab again,onCreateView() is called again and view is repopulated which slows down and blocks UI thread.

  • Changing Navigation drawer hamburger icon
  • Setting Application theme textColor to white causes Context Menu item text to be white (invisible)
  • Making a nav bar disappear in Xamarin.Forms
  • Difference between DeadObjectException and causing a NullPointerException
  • The following classes could not be found: android.support.v7.internal.app.WindowDecorActionBar
  • Why is wrap content in multiple line TextView filling parent?
  • What is the correct way to create fragments inside tabs ONCE and avoid recreating their view when swiping between other tabs?

    Related posts:

    Android: QCMediaPlayer could not be located
    TextInputLayout animation overlaps the text when the text is set programmatically
    How to disable ScrollView scrolling?
    “computeValuesWithHarfbuzz — need to force to single run” in Android 4...
    Custom Fonts and Custom Textview on Android
    Up arrow does not show after calling ActionBarDrawerToggle.setDrawerIndicatorEnabled(false)
  • Spacing Issue while creating bus layout in recycler view Gridlayout manager
  • Jacoco Code Coverage in android studio
  • Android Audio - Streaming sine-tone generator odd behaviour
  • Android :EditText loses content on scroll in ListView?
  • Android Device not recognized by adb
  • java.lang.NoSuchMethodError: No static method setOnApplyWindowInsetsListener
  • 3 Solutions collect form web for “how to avoid onCreateView() when swiping in tabs”

    To add to what CommnosWare stated, if you are using a view pager, you can simply call mViewPager.setOffscreenPageLimit(3); to keep all three in memory. But if your UI is too heavy, you may notice some jank. So try revising your UI setup code.

    I have some heavily created UI elements inside tabs which take some time to be created

    If “some time” is more than ~5 milliseconds, your app is broken and needs to be fixed. Use Traceview to determine precisely where you are spending your time, then repair matters so that it takes less time on the main application thread.

    What is the correct way to create fragments inside tabs ONCE and avoid recreating their view when swipping between other tabs?

    AFAIK, FragmentPagerAdapter should not have the effect that you are describing (though FragmentStatePagerAdapter will). But, again, if you fix your onCreateView() performance issue, even FragmentStatePagerAdapter will not post a problem.

    Just add the following code inside main Fragment/Activity class where TabAdapter declared.

    viewPager.setOffscreenPageLimit(tabLayout.getTabCount());
    

    The tabLayout and viewPager as follows

    TabLayout tabLayout = (TabLayout) root.findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.