Reputation: 245
I have a question, a tab bar at the bottom of MainActivity
(extends FragmentActivity
),
when I launch the app, the fragment1(in tab1) and fragment2(in tab2) will call onCreateView
,
and some loading operation(like scan something) in each fragent, but I don't know how to
stop call onCreateView
when I switch two tabs, I just want to keep the loading state even I switch one from another.
How do I implement it (just use this way, I don't want to use tabhost)?
Upvotes: 6
Views: 6603
Reputation: 12341
If you call setOffscreenPageLimit(2)
on your ViewPager
, onCreateView
would only be called at the start.
Upvotes: 5
Reputation: 3536
Fragment.onCreateView
is standard part of fragment lifecycle and you should not be trying to prevent it from being called.
From what i understand, your onCreateView method is probably doing some ugly work and that is why you dont want it to be called. If that is the case, you should move this code to another lifecycle method, that will not be called so often (for example onCreate, or onActivityCreated, if you also need your activity context).
onCreateView should be only used to inflate your layout, and possibly make references to ui components that you will need to interact with.
Upvotes: 6
Reputation: 51
begin your loading in onCreate or onAttach method,then it will not be restarted with onCreateView. http://developer.android.com/guide/components/fragments.html this page have the fragment life cycle.
Upvotes: 0