The life cycle of fragment in Android and the management of return stacks _android

Source: Internet
Author: User

Now that we know that fragment is very useful, we need to know how it works. Fragment can only exist in (as a container) activity, each fragment has its own view structure that can be loaded into the layout as we did before. The life cycle of fragment is more complex because it has more states, as shown in the figure:

Let's take a look at the complete life cycle of fragment.

The Oninflate method is invoked at the beginning of the fragment lifecycle. Note that this method is invoked only when we define it directly in the layout file. We can save some of the configuration parameters and some properties defined in the XML layout file in this method.
This step is followed by a call to Onattach. This method is invoked when fragment is bound to its parent activity, where we can save the reference between it and the activity.
OnCreate will be invoked after that. This is one of the most important steps. Fragment is generated in this step, and you can use this method to initiate other threads to retrieve data, such as booting from a remote server.
Oncreateview This method is invoked when fragment creates its own view structure, in which we load the fragment layout file, just as we load the layout in the ListView control. In this process, we cannot guarantee that the parent activity has been created, so there are some operations we cannot complete here.
You can see that after onactivitycreated the activity is only established. By this step, our activity is created and activated. We can use it anytime.
The next step is OnStart, where we do things like the onstart in the activity, where fragment can be displayed but not interact with the user. Only after Onresume is fragment can the user be started to interoperate. After this process, the fragment has been started and run.
The activity may be suspended. The OnPause method of the activity is invoked. The fragment OnPause method is also invoked. The
system may also destroy fragment view display, when this occurs, the Ondestroyview method is invoked. After
, the OnDestroy method is invoked if the system needs to completely destroy the entire fragment. At this point we need to release all the connections that are available, because fragment will be killed soon. Although it is in the process of preparing for destruction, fragment is still bound to the parent activity.
The last step is to fragment the activity from the activity, that is, to invoke the Ondetach method.

Management of Fragment return stack
to add fragment to the return stack:

Assuming we now have two fragment:fragment01 and Fragment02, we now jump from the Fragment01 interface to the Fragment02, then press the back key to find out that the program is exiting directly instead of returning to the Fragment01. If you want to do this now: Jump from the Fragment01 interface to Fragment02 and press the back key to return to Fragment01. How should this function be implemented? This actually utilizes the knowledge that returns the stack.

In fact, the fragmenttransaction provides a addtobackstack () method that can add a transaction to the return stack.

Here we can add fragment to the return stack by adding one line of code, based on a dynamically loaded fragment code:

Step One: Add an instance of Fragmenttransaction
fragmenttransaction transaction = Getfragmentmanager (). BeginTransaction ();
Step Two: Use the Add () method plus fragment Object
rightfragment rightfragment = new Rightfragment ();
Transaction.add (R.id.right, rightfragment);
Transaction.addtobackstack (null);
Step three: Invoke the Commit () method to make the Fragmenttransaction instance change effective
transaction.commit ();

We called the Fragmenttransaction Addtobackstack () method before the transaction was committed, and it can accept a name to describe the state of the return stack, generally passing in null.

Example

@Override public
   void OnClick (View v) {
     //TODO auto-generated method stub
 Transaction = Manager.begintransaction (); The     switch (V.getid ()) {case
     r.id.button1:
       Fragment01 fragment01 = new Fragment01 ();
       Transaction.replace (R.id.right, fragment01, "fragment01");
       Transaction.addtobackstack ("fragment01");//Added to the fallback stack of activity management. Break
       ;
 
     Case R.id.button2:
       Fragment02 fragment02 = new Fragment02 ();
       Transaction.replace (R.id.right, fragment02, "fragment02");
       Transaction.addtobackstack ("fragment02");//Added to the fallback stack of activity management. Break
       ;
 
     Case R.id.button3:
       Fragment03 fragment03 = new Fragment03 ();
       Transaction.replace (R.id.right, fragment03, "fragment03");
       Transaction.addtobackstack ("fragment03");//Added to the fallback stack of activity management. Break
       ;
 Transaction.commit ();   
   }

After running the program, the interface is as follows, without any fragment being loaded:

Click the button to load fragment01:

Click the button to load fragment02 (this time the fragment01 is replaced and pressed into the stack):

Note: If the fragment01 is not pressed onto the stack during the replacement, it will be destroyed and the OnDestroy () and Ondetach () methods will continue after the Ondestroyview () method is executed.

Press the back key to return to the screen fragment01: (fragment02 destroyed)

Press the back key again and the fragment01 is destroyed:

Note: The return stack of the fragment is managed by activity, while the return stack of the activity is managed by the system.

Related Article

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.