Problem analysis
has been in the book to see other people's technical stickers, today I also write some of their own experience! Recently in writing a project with a lot of fragment after the summary!
I think the students who have just contacted Android may write:
Fragmentmanager fragmentmanager=getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=fragmentmanager.begintransaction (); Fragmenttransaction.add ( viewid,fragment); // or Fragmenttransaction.replace (viewid,fragment); Fragmenttransaction.commit ();
A better-based homecoming with show and Hide methods
Fragmentmanager fm == fm.begintransaction (); Ft.hide (new firstfragment ()) . Show ( New secondfragment ()) . commit ();
Admittedly these two can switch fragment, but the face of the user a large number of clicks back and forth, or your fragment is a lot, every time this operation, then your application will be oom, even if not the collapse that will be abnormal stutter! So why?
When we replace, the following life cycle occurs:
Think about it and replace it every time!! How wonderful the world will be!!! So what's the problem? Looking back at the code will find that every time a new instance is Add/replace or show/hide, that's the fatal reason !!!!!
Cut the crap, get started. Optimization Scheme one:
Pre-load Mode:
First of all, we need to instantiate three global fragment
Fragmentmanager fm = Getsupportfragmentmanager ();
Fragmenttransaction ft = fm.begintransaction ();
Ft.add (R.id.fragment, Firstfragment.getinstance ());
Ft.add (R.id.fragment, Secondfragment.getinstance ());
Ft.add (R.id.fragment, Thirdfragment.getinstance ());
Ft.hide (Secondfragment.getinstance ());
Ft.hide (Thirdfragment.getinstance ());
Ft.commit ();
Load all fragment when loading the first fragment, and the next time you use a direct call such as:
Fragmentmanager fm = Getsupportfragmentmanager ();
Fragmenttransaction ft = fm.begintransaction ();
Ft.hide (Firstfragment.getinstance ())
. Show (Secondfragment.getinstance ())
. commit ();
Is not always feel strange, although better than the previous code, but this approach is Java, of course, the need to preload friends is still the best choice!!!
Is there a better way to do that? The answer is yes.
Scenario Two:
Dynamic load Mode:
//first, we need to instantiate a good n global fragment//Private Fragment currentfragment=new Fragment (); (global)Privatefragmenttransaction switchfragment (Fragment targetfragment) {fragmenttransaction transaction=Getsupportfragmentmanager (). BeginTransaction (); if(!targetfragment.isadded ()) { //currentfragment is null when using Switchfragment () for the first time, so make a judgment if(Currentfragment! =NULL) {transaction.hide (currentfragment); } transaction.add (R.id.fragment, Targetfragment,targetfragment.getclass (). GetName ()); } Else{transaction. Hide (Currentfragment). Show (Targetfragment); } currentfragment=targetfragment; returntransaction; When you click Switch Fragment: @Override Public voidOntabselected (@IdResinttabId) { if(TabId = =R.id.tab_one) {switchfragment (first). commit (); } if(TabId = =r.id.tab_two) {switchfragment (second). commit (); } if(TabId = =R.id.tab_three) {switchfragment (third). commit (); } }
Now your fragment no matter how the cut will not appear in Dayton, because all your fragment will only be instantiated once! Once the instance of the fragment will be stored in memory, the next switch will determine whether the memory contains the fragment to switch, if there is a direct reuse, no add a new! Optimization Dafa Complete!
Wai Fan
What? Wait a minute! Just one instance at a time, what if the data in my fragment is to be updated? My answer is--the software is shut down and restarted again!
If so, this kind of software really has to reverse the sky! Fortunately, the official provides the Onhiddenchanged method, each time you switch hide or show the method will be executed, you can update the data here!
This method is used in fragment
@Override
public void Onhiddenchanged (Boolean hidden) {
Super.onhiddenchanged (hidden);
if (hidden) {
Fragment called when hidden
}else {
Called when fragment is displayed
}
}
This method is not much more elegant than performing a large lump of the life cycle of each add or replace update data!
GitHub Address: Fragmentdemo (Welcome to fork and Star)
Note: Remind small white (veteran please ignore)
This demo is only for fragment understanding, the business logic of this sample app is recommended viewpager+fragment or other ...
8 Jinmu 8
Links: https://www.jianshu.com/p/4c5f015b3b6c
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
Multi-fragment switching optimization for Android development