5

I explored few tab fragment example (Provided in Support4Demos one also) But I found that every time tab is switched, the tab content view is created each time from 'onCreateView' of the 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 fragment views once when they are first created and will be shown/gone when switched between tabs instead of creating again?

StarDust
  • 846
  • 1
  • 13
  • 26

2 Answers2

1

I think I found the solution. I need to use pager, which caches the tab and doesn't create new view each time it is switched.

Found it from here: How to cache a fragment view

Community
  • 1
  • 1
StarDust
  • 846
  • 1
  • 13
  • 26
0

No, you gotta get used to this idea and start saving important information for screen rotations and similar. Then when you create the view again you take the saved information and use it for initialization.

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

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • So fragment tab is not a good idea to use to serve my purpose :S I have heavy tab contents, I don't want to create them every time I switch back. Any solution if I want to to create a tab content view only once and use it when tab is switched? – StarDust Feb 28 '13 at 05:39