Snippet tab, View creation every time you switch tabs

I looked at a few examples of tab fragments (also in Support4Demos), but I found that every time a tab is switched, a tab content presentation is created each time from the 'onCreateView' fragment class.

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_world, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("Fragment #" + mNum); tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); return v; } 

Is it possible to create kinds of fragments once, when they are first created, and will be displayed / deleted when switching between tabs instead of creating again?

+4
source share
2 answers

I think I found a solution. I need to use a pager that caches the tab and does not create a new view every time I switch.

Found it from here: How to cache fragment view

0
source

No, you should get used to this idea and start saving important information for screen rotation and the like. Then, when you re-create the view, you take the stored information and use it to initialize.

See here for a similar discussion. Mostly in your onActivityCreated fragments you load data, and in your onSaveInstanceState fragments you save data.

0
source

All Articles