Guo Xiaoxing
Weibo: Guo Xiaoxing's Sina Weibo
Email:[email protected]
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwells
If our app is designed to support a wide range of screen sizes, within the available screen space, we can optimize our user experience by reusing your fragment in different layout configurations. A mobile device may be suitable for single-panel user interactions with only one fragment at a time. On the other hand, on larger screen-sized tablets, we might want to have two fragment side-by-side to show more information to the user.
As shown in the following:
Fragment is part of the activity modularity, which has its own lifecycle, receives its own input events, can be added and removed from activity operations, and can be reused by multiple activity.
Characteristics of a fragment
Overall, fragment has the following features:
- Fragment is always part of the activity interface, fragment can call the Getactivity () method to get the activity it is in. Activity can be obtained by Fragmentmanager's Findfragmentbyid () method or Findfragmentbytag () method to obtain the corresponding fragment.
- During activity operation, fragment can be added, removed, or replaced by Fragmentmanager's add (), remove (), and replace () methods.
- An activity can use multiple frameworks at the same time, and the same fragment can be reused by multiple activity.
- Fragment can respond to its own input events and have its own life cycle, but the fragment life cycle is directly controlled by the activity it belongs to.
The life cycle of the two fragment and the fragment of the callback method
The life cycle of the fragment is as follows:
- Active Status: The current fragment is located in the foreground, the user can be visible, the focus is available.
- Paused state: Other activity is located in the foreground, fragment is still visible, just can't get focus.
- Stop state: Fragment not visible, loses focus.
- Destroy state: Fragment is completely deleted, or the activity where the fragment is located is ended.
callback method for Fragment
The callback method for fragment is as follows:
- Onattach (): When fragment is added to the activity, it is called back, and the method is only invoked once.
- OnCreate (): This method is called back after the system creates the fragment object, so that only the necessary components that need to be persisted in fragment are initialized in the code, and can be resumed when the fragment is paused or stopped. The method will only be called once.
- Oncreateview (): When fragment draws the interface component, the method is called back, and the method returns a view, which is the view displayed by the fragment.
- Onactivitycreated (): Callback The method when the activity that the fragment is in is started.
- Onresume (): The Onresume () method will be called after the OnStart () call is recalled when fragment is resumed.
- OnPause (): The method is recalled when the user leaves the fragment.
- Ondestory (): The fragment is called back when it is destroyed, and the method is only invoked once.
- Ondetach (): The fragment is removed from the activity, the method is called back when the substitution is completed, and the Ondestory () method call will definitely callback the Ondetach () method, which is only called once.
Here we show an example of the fragment life cycle and the invocation process of the callback method.
The main program interface is as follows:
The various callback methods of the custom fragment are added to the print to observe the change of the callback method, the source code is as follows:
Fragment life cycle and callback method demo use case download
Lifecyclefragment Source
PackageOrg.crazyit.app;Importandroid.app.Activity;ImportAndroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public class lifecyclefragment extends Fragment{ FinalString TAG ="Lifecyclefragment";@Override Public void Onattach(Activity activity) {Super. Onattach (activity);//output logLOG.D (TAG,"-------onattach------"); }@Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//output logLOG.D (TAG,"-------onCreate------"); }@Override PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle data) {//output logLOG.D (TAG,"-------oncreateview------"); TextView TV =NewTextView (Getactivity ()); Tv.setgravity (gravity.center_horizontal); Tv.settext ("Test Fragment"); Tv.settextsize ( +);returnTv }@Override Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate);//output logLOG.D (TAG,"-------onactivitycreated------"); }@Override Public void OnStart() {Super. OnStart ();//output logLOG.D (TAG,"-------onStart------"); }@Override Public void Onresume() {Super. Onresume ();//output logLOG.D (TAG,"-------onresume------"); }@Override Public void OnPause() {Super. OnPause ();//output logLOG.D (TAG,"-------onPause------"); }@Override Public void OnStop() {Super. OnStop ();//output logLOG.D (TAG,"-------onStop------"); }@Override Public void Ondestroyview() {Super. Ondestroyview ();//output logLOG.D (TAG,"-------ondestroyview------"); }@Override Public void OnDestroy() {Super. OnDestroy ();//output logLOG.D (TAG,"-------ondestroy------"); }@Override Public void Ondetach() {Super. Ondetach ();//output logLOG.D (TAG,"-------ondetach------"); }}
First click Load Target fragment
After entering the application, we first click Load target fragment, we will find that fragment will Onattach () added to the current activity, after the creation is complete call Onresume (), the function call chain as shown:
Second Click to load target fragment
When we click Load fragment again, the original fragment will be destroyed, the new fragment will be created, and the function call chain is as follows:
Click Start dialog style activity
When fragment has been created, we then click Start dialog Style activity, after the dialog box is started, activity is in pause state, then the fragment belonging to it will naturally be in the pause state, as shown in:
When the dialog box exits, activity resumes, and fragment is restored as shown:
Click Replace target fragment and join the back stack
When we click on Replace target fragment and add back stack, at this time fragment although not visible, but not destroyed, it is just added to the background of the backend stack, as shown in:
When the user presses the back key, the fragment added to the return stack is displayed again, as shown in:
Click to exit
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android App Development technology: Application Components" Fragment fundamentals