Android--Fragment precautions

Source: Internet
Author: User

viewpager+fragment

When fragment becomes a page of Viewpager, Fragmentmanager will always save the fragment created by management, even if it is not currently displayed, fragment objects will not be destroyed, silently waiting for re-display in the background. However, if fragment is no longer visible, its view hierarchy is destroyed and the view is recreated the next time it is displayed.

fragment objects persist in memory for use with Fragmentpageradapter , so when there are a large number of display pages, it is not suitable for fragmentpageradapter. Fragmentpageradapter applies to only a few page cases like tabs. This time you can consider using fragmentstatepageradapter , when using Fragmentstatepageradapter, If fragment is not displayed, then the fragment object will be destroyed , but the onsaveinstancestate (bundle Outstate) method is called back before the callback OnDestroy () method to save the fragment state , and the next time fragment is displayed by OnCreate (bundle Savedinstancestate) Take out the stored state values, fragmentstatepageradapter more suitable for the page compared to the situation, like a page of the ListView.

Animation
1 fragmenttransaction ft =23ft.replace (r.id.details, detail);         4 ft.addtobackstack (null5 ft.commit ();

The fade (gradient) effect of the code setting.

1 fragmenttransaction ft = getfragmentmanager (). BeginTransaction (); 2 // setcustomanimations () must be located before replace (), otherwise the effect is not up. Its two parameters are enter,exit effect respectively. The system currently offers two effects, namely Android. R.animator.fade_in and Android. R.animator.fade_out3ft.setcustomanimations (r.animator.slide_in_left,r.animator.slide_out_ right); 4 ft.addtobackstack (null); 5 ft.replace (r.id.details, "detail"); 6 ft.commit ();

Using Setcustomanimations ()

parameterless Constructors & Pass Parameters

Fragment must have a parameterless constructor, if you want to give fragment, you must use bundle method to pass the parameter, and do not overload the constructor argument, because the use of fragment regeneration will not execute the parameter constructor, but instead of executing the parameterless constructor.

1  Public StaticVechilefrag newinstance (Vehicle Vehicle, String userId,BooleanIsadd) {2Vechilefrag MF =NewVechilefrag ();3Bundle args =NewBundle ();4Args.putstring ("UserId", userId);5Args.putboolean ("Isadd", Isadd);6Args.putparcelable ("Vehicle", vehicle);7 mf.setarguments (args);8     returnMF;9 }Ten  One  A @Override -  Public voidonCreate (Bundle savedinstancestate) { -     Super. OnCreate (savedinstancestate); theBundle args =getarguments (); -     if(Args! =NULL) { -UserID = args.getstring ("userid"); -Isadd = Args.getboolean ("Isadd"); +Vehicle = args.getparcelable ("Vehicle"); -         if(Vehicle = =NULL) { +Vehicle =NewVehicle (); A         } at     } -}
menu                                                                                        
1 @Override 2      Public void onCreate (Bundle savedinstancestate) {3         Super . OnCreate (savedinstancestate); 4         Sethasoptionsmenu (true); 5     }

After that, you can call Oncreateoptionsmenu.

Save the fragment switch
Fragment Fragment =Fragments.get (i);if(!fragment.isadded ()) {    if(Currentindex!=-1) Fragments.get (currentindex). OnPause (); FT=fm.begintransaction ();    Ft.settransition (Android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE);    Ft.add (r.id.framelayout,fragment); //ft.addtobackstack (null);Ft.commit (); Currentindex=i;}Else{ft=fm.begintransaction ();    Fragments.get (Currentindex). OnPause ();    Ft.settransition (Android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE);    Ft.hide (Fragments.get (currentindex));    Ft.show (Fragments.get (i));    Fragments.get (i). OnStart ();    Ft.commit (); Currentindex=i;}

I'm the dividing line of the king of the land Tiger.

Android--Fragment precautions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.