Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid fragment oncreateview called on tab change
    text
    copied!<p>I've been working with Android for a while, but fragments are a little new to me (as they are to most people probably). Anyway, I've got the below code, and it works fine. I've got three fragments, one in each tab. I'm wondering if it's normal for onCreateView to be called everytime I switch tabs, and does it make sense to do so? Shouldn't there be a way to NOT redraw the fragment every time the tab changes?</p> <p>I'm converting this from an app that had 3 activities, one in each tab, and it seems like a waste to recreate the view every time the tab changes, when it used to be just fine having the views exist between tab changes...</p> <p>BTW, this code borrowed from: <a href="http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/">http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/</a></p> <pre><code>public class Tabs extends FragmentActivity implements TabHost.OnTabChangeListener { final String MAP_TAB = "Map"; final String IMAGES_TAB = "Images"; final String SETTINGS_TAB = "Settings"; TabHost mTabHost; HashMap&lt;String, TabInfo&gt; mapTabInfo = new HashMap&lt;String, TabInfo&gt;(); TabInfo mLastTab = null; private class TabInfo { private String tag; private Class clss; private Bundle args; private Fragment fragment; TabInfo(String tag, Class clazz, Bundle args) { this.tag = tag; this.clss = clazz; this.args = args; } } class TabFactory implements TabContentFactory { private final Context mContext; public TabFactory(Context context) { mContext = context; } public View createTabContent(String tag) { View v = new View(mContext); v.setMinimumWidth(0); v.setMinimumHeight(0); return v; } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); initialiseTabHost(savedInstanceState); if (savedInstanceState != null) mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state } protected void onSaveInstanceState(Bundle outState) { outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected super.onSaveInstanceState(outState); } private void initialiseTabHost(Bundle args) { mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost.setup(); TabInfo tabInfo; Tabs.addTab(this, mTabHost, mTabHost.newTabSpec(MAP_TAB).setIndicator( MAP_TAB, getResources().getDrawable(R.drawable.ic_tab_map_states)), ( tabInfo = new TabInfo(MAP_TAB, HMapFragment_NEW.class, args))); mapTabInfo.put(tabInfo.tag, tabInfo); Tabs.addTab(this, mTabHost, mTabHost.newTabSpec(IMAGES_TAB).setIndicator( IMAGES_TAB, getResources().getDrawable(R.drawable.ic_tab_gallery_states)), ( tabInfo = new TabInfo(IMAGES_TAB, ImageGridFragment.class, args))); mapTabInfo.put(tabInfo.tag, tabInfo); Tabs.addTab(this, mTabHost, mTabHost.newTabSpec(SETTINGS_TAB).setIndicator( SETTINGS_TAB, getResources().getDrawable(R.drawable.ic_tab_settings_states)), ( tabInfo = new TabInfo(SETTINGS_TAB, SettingsFragment.class, args))); mapTabInfo.put(tabInfo.tag, tabInfo); // Default to first tab this.onTabChanged(MAP_TAB); mTabHost.setOnTabChangedListener(this); } private static void addTab(Tabs activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { // Attach a Tab view factory to the spec tabSpec.setContent(activity.new TabFactory(activity)); String tag = tabSpec.getTag(); // Check to see if we already have a fragment for this tab, probably // from a previously saved state. If so, deactivate it, because our // initial state is that a tab isn't shown. tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag); if (tabInfo.fragment != null &amp;&amp; !tabInfo.fragment.isDetached()) { FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); ft.detach(tabInfo.fragment); ft.commit(); activity.getSupportFragmentManager().executePendingTransactions(); } tabHost.addTab(tabSpec); } public void onTabChanged(String tag) { TabInfo newTab = this.mapTabInfo.get(tag); // if they've clicked to change tabs if (mLastTab != newTab) { FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); if (mLastTab != null) if (mLastTab.fragment != null) ft.detach(mLastTab.fragment); if (newTab != null) { if (newTab.fragment == null) { newTab.fragment = Fragment.instantiate(this, newTab.clss.getName(), newTab.args); ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); } else ft.attach(newTab.fragment); } mLastTab = newTab; ft.commit(); this.getSupportFragmentManager().executePendingTransactions(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload