Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop call onCreateView when switch different tab(like tab1, tab2) in android?

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)?

like image 696
Lijun Lou Avatar asked Oct 25 '13 07:10

Lijun Lou


2 Answers

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.

like image 89
hendrix Avatar answered Nov 14 '22 23:11

hendrix


If you call setOffscreenPageLimit(2) on your ViewPager, onCreateView would only be called at the start.

like image 21
Ε Г И І И О Avatar answered Nov 15 '22 00:11

Ε Г И І И О