Currently, we have implemented a tab, and we are drawing a frame on Frame Layout and a view as a replace with Fragment Transaction. Both fragments are bringing in items from the server call. Every time it is replaced, the onCreateView is called again and keeps sending server calls. Is there a way to bring items from the server only when it is first replaced without being destroyed?
Or is it better to change the structure from activity to server call when the structure is now fragmented?
android fragmenttransaction lifecycle android-viewpager
Are you manually implementing tabs and managing them with FragmentTransaction
? If so, it would be better to try FragmentTabHost. Fragments can be placed on multiple tabs and will not require transactions of fragments to move to different tabs.
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"), LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
Since you said 2 fragments, maybe using FragmentTabHost
won't cause excessive server calls.
© 2024 OneMinuteCode. All rights reserved.