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)?
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.
If you call setOffscreenPageLimit(2)
on your ViewPager
, onCreateView
would only be called at the start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With