- Problem description
Most of the time we use fragment are nested inside the Viewpager fragment, or activity under a selection tag, in a container to add a few fragment to switch, this time between the various fragment switch , how their life cycle changes, let's study together.
- Solution Solutions
fragment nested inside the Viewpager .
The demo was down on the CSDN, where the original author was posted. (http://blog.csdn.net/u013758734/article/details/29848117)
We add log output to all life cycle methods in four fragment.
- when we open the demo for the first time, we check the log output
We found that the five life cycle methods of onefragment and Twofragment were executed sequentially. Understand: Because Viewpager will actually cache the left and right pages, the default cache is 2 pages. At this point the twofragment is actually ready, just not appearing on the right side of the screen.
- when we slip down by onefragment twofragment
We found that threefragment walked 5 life cycle methods, while onefragment did not change. Understanding: Viewpager will cache the left and right pages, so onefragment has not changed, just loaded threefragment.
- when we slip down by twofragment threefragment
We found that Onefragment was destroyed first and then Fourfragment was loaded. Understanding: Viewpager will cache the left and right pages, and when on the third page, only the second and fourth pages are cached.
See these three should be able to know and Threefragment slip fourfragment How to change it, Twofragment will be destroyed.
Summary: Viewpager will cache the left and right pages by default, so the life cycle that has been cached is the same, and the life cycle is in the same state.
Since the default viewpager is to cache two pages, there must be a way to set the number of cache pages Ah, that is:
mViewPager.setOffscreenPageLimit(3);
There are 4 pages, so you need to cache 3 pages. When this method is set, let's look at it and go to the demo:
We will find that the fourfragment is also directly cached, which is consistent with the theory, as to what the order of the cycle can be understood by you. However, no matter how slippery the 4 pages, the life cycle has not changed, then there is a problem here, when I entered the first page, the other pages are loaded, so that a lot of resources to load, how to optimize the problem. We'll talk about this next. Let's take a look at this viewpager is obscured by activity and changes when exiting.
- when you press the home key, the picture is obscured by other activity (excluding Dialog,activitydialog)
Understanding: In this case its life cycle changes with the activity, and all the fragment have a consistent life cycle.
- Re-entry demo
- Exit Demo
Having said so much, the analysis of Viewpager nested fragment its life cycle changes, when we set the Setoffscreenpagelimit (3) This, enter this Viewpager, All the fragment will go into the Onresume state, the equivalent of all the data loaded, when we have a lot of pages or a lot of data, the burden on the application, how to deal with this problem.
Fragment provides us with the Setuservisiblehint (Boolean Isvisibletouser) method
We add this method to each fragment, such as
@Override publicvoidsetUserVisibleHint(boolean isVisibleToUser) { if (isVisibleToUser) { Log.i("TAG""OneFragment--setUserVisibleHint"); } super.setUserVisibleHint(isVisibleToUser); }
when we get to the demo,
Found that the first execution is the Setuservisiblehint method, understand: This method is when fragment appears on the screen, the Isvisibletouser parameter will be set to true, so we can not use this method well, We can write the method of loading the data in this method .
enter several other fragment in turn, and its Setuservisiblehint method will be executed sequentially
Summary : Encountered viewpager inside nested fragment time, Setuservisiblehint and Setoffscreenpagelimit methods can be used in conjunction with the use, so as to maximize the optimization of this module.
Above is the viewpager inside nested fragment when, when in a container inside add to switch several fragment, this time each fragment between the switch, this time life cycle how to change it, Let's put a code that sets a few fragment in a container.
Fragmenttransaction transaction = Fragmentmanager.begintransaction (); Hidefragments (transaction);Switch(Index) { Case 2:if(Myinfofragment = =NULL) {myinfofragment =NewMyinfofragment (); Transaction.add (R.id.content, myinfofragment); }Else{transaction.show (myinfofragment); } Break; Case 0:if(Pharmacistfragment = =NULL) {pharmacistfragment =NewPharmacistfragment (); Transaction.add (R.id.content, pharmacistfragment); }Else{transaction.show (pharmacistfragment); } Break; Case 1:if(Findfragment = =NULL) {findfragment =NewFindfragment (); Transaction.add (R.id.content, findfragment); }Else{transaction.show (findfragment); } Break;default: Break; } transaction.commit ();
This is similar to the Viewpager nested fragment life cycle, the only difference is that when you go to the first page, the default will not be the other two pages cache (of course you can write it), when the other two pages, the cache is good, its life cycle changes are exactly the same, The life cycle of all fragment is in one state. Of course, also configure the Setuservisiblehint method to use, optimize this module. Specific demo demo, I will not do, you can do it yourself.
Have any questions welcome message
Shangyuan yards http://download.csdn.net/detail/xiangxi101/9490794
/**
* --------------
* Welcome Reprint | Reprint Please specify
* --------------
* If you are helpful, please click on the top |
* --------------
* Please remain humble | You will go farther
* --------------
* @author CSS
* @github Https://github.com/songsongbrother
* @blog http://blog.csdn.net/xiangxi101
*/
Android Fragment life cycle applications