If you replace the fragment in Android Fragment Transaction, the fragment life cycler is destroyed and onCreate, so can you control this part? Don't let the gc eat you (so that you don't destroy me)

Asked 2 years ago, Updated 2 years ago, 116 views

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

2022-09-21 18:05

1 Answers

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.


2022-09-21 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.