1. Preface
Yesterday we talked about the encapsulation of baseactivity,
Android talk about encapsulating those things--baseactivity and basefragment (i)
There are a lot of tips from small partners, such as:
- Universal title bar allows you to customize the view without placing it in base, the code is more uniform
- The baseeventactivity should be set aside to ensure that no bus activity is used.
- Basestatusactivity There's just one way. No need to create a new
- and some small details.
Thank you for your advice here. I modified the part, and the latter will slowly optimize, and finally put the best code in the Hlibrary. and the basestatusactivity (immersion bar) will continue to be optimized. Next talk about Basefragment packaging, in fact, is mainly about fragment lazy loading problem.
2.Fragment Related knowledge points
- Life cycle
- Use of fragment: static, dynamic
- Fragment issues to be noted
- ......
Here is not the detailed introduction, want to know can read my this article:
Are you really going to use fragment? -fragment parsing
1.Fragment Usage Scenarios
We can see the market app is generally used to switch the bottom tab+fragment to the whole shelf, or through the Viewpager as a container nesting fragment, and then more complicated is fragment nested fragment, a book, a piece and so on;
2. Problems encountered
In this case, the problem we encountered is when fragment a lot of combinations used, each fragment inside will load data or perform animation and other complex business logic, resulting in our app when the screen lag, performance is poor, not smooth. Even if we are using Viewpager as a container to set the preload Setoffscreenpagelimit () This method, you will actually find that it will not work at all.
As for Viewpager this preload method why not? We can look at the Viewpager source code:
We can see default_offscreen_pages here defines the default value is 1, if you call the method passed in the value of less than 1 is invalid, will be forcibly dragged back to 1. And default_offscreen_pages This value is private, subclass inheritance Viewpager is also not visible.
Online some said can be Viewpager source copy down to their own class inside the default value is changed to 0, this is not yet tried, even if you can feel a bit awkward, not to mention we do not need to use the scene Viewpager it
3.Fragment Lazy Loading
This is a method of Setuservisiblehint (Boolean isvisibletouser) in fragment, which we can judge in this method, when its true is visible (that is, to switch to a specific fragment) to load the data , so you can save traffic.
Specifically how to operate it?
- Pre-load lightweight operations such as initializing data and components
- To perform a network request when you toggle tab to the first time visible
- Four states: First visible state, visible state, first invisible State, invisible State
- Understanding registration and destruction of broadcasts at the time of destruction
3.BaseFragment Encapsulation 1. Initializing an XML file
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (getContentViewLayoutID() != 0) { return inflater.inflate(getContentViewLayoutID(), null); } else { return super.onCreateView(inflater, container, savedInstanceState); } } protected abstract int getContentViewLayoutID();
2. Annotation binding and initialization of components
@Override void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ButterKnife.bind(this,view); initViewsAndEvents(view); } protected abstract void initViewsAndEvents(View view);
3. Four types of "see" state splits
@Override Publicvoid onactivitycreated (Bundle savedinstancestate) {Super.onactivitycreated (savedinstancestate); Initprepare (); } Private Synchronizedvoid Initprepare () {if (isprepared) {onfirstuservisible (); }else {isprepared =True }} @Override Publicvoid Setuservisiblehint (Boolean isvisibletouser) { super.setuservisiblehint (isvisibletouser); if (isvisibletouser) { if (isfirstvisible) {isfirstvisible = false; Initprepare ();} else {onuservisible ();}} else { if (isfirstinvisible) {isfirstinvisible = false; onfirstuserinvisible ();} else {onuserinvisible ();}} } protected abstract void onfirstuservisible (); protected abstract void onuservisible (); private void OnFi Rstuserinvisible () {} protected abstract void onuserinvisible ();
4. The final solution to the registration of destruction and other work
@Override void onDestroy() { DetoryViewAndThing(); super.onDestroy(); } protected abstract void DetoryViewAndThing();
5. Jump interface and other methods
/** * Open an activity by default does not close the current activity */public
void gotoactivity (class<?> clz) {gotoactivity (CLZ,
false, null); } public void gotoactivity (class<?> clz, Boolean isclosecurrentactivity) {GotoActivi Ty (Clz, isclosecurrentactivity, null), public void Gotoactivity (Class<?> clz, Boolean isclosecurrentactivity, Bundle ex) {Intent Intent = new Intent (mactivity, CLZ); if (ex! = null) Intent.putextras (ex); startactivity (intent); Span class= "Hljs-keyword" >if (isclosecurrentactivity) {mactivity.finish ();}}
Finally, a small question: that is, if you do not use fragment lazy load, but the use of Viewpager preload, if you do not define the default preload number, then the default will certainly be loaded in advance, Join your current fragment adjacent to the fragment inside there is animation or video playback, switch to the current fragment time the next fragment inside the animation or video has begun to execute, pay attention to.
To this my base on the end, a lot of unreasonable places, I will be in private to listen to the advice of the small partners to modify, and then finally posted in Hlibrary, thank you very much.
Android Talk about encapsulation those things--baseactivity and basefragment (ii)