Android_fragment life cycle and return stack Backstack

Source: Internet
Author: User

A preliminary study on the life cycle of fragment

Because fragment must be embedded in the acitivity, the life cycle of fragment is closely related to the activity in which it resides.

If the activity is paused, all of the fragment are paused, and if the activity is stopped state, all fragment in the activity cannot be activated, and if the activity is destroyed, Then all of its fragment will be destroyed.

However, when activity is active, you can independently control the state of the fragment, such as adding or removing fragment.

When this is done fragment transaction (conversion), you can put the fragment into the activity's back stack , so that the user can return the operation.

When using fragment, you need to inherit fragment or fragment subclasses (Dialogfragment, Listfragment, Preferencefragment, webviewfragment), So fragment's code looks similar to the activity.

Each time a fragment is created, the following three callback methods are added first:

    • OnCreate (): The system calls this method when creating the fragment, where the related components should be initialized, and some things that need to be preserved even if they are paused or stopped.
    • Oncreateview (): When the fragment UI is first drawn, the system calls this method, which returns a view that can return NULL if fragment does not provide a UI. Note that if you inherit from Listfragment,oncreateview () The default implementation returns a ListView, so you don't have to implement it yourself.
    • OnPause (): When a user leaves fragment, the first call to this method requires a change to be committed because the user is likely to no longer return.

There are two ways to load fragment into activity:

    • Method One: Add fragment to the activity's layout file
    • Mode two: Dynamically add fragment (recommended) in the activity's code

The first approach is simple but not flexible enough. Adding fragment to the activity's layout file is equivalent to binding fragment and its views to the activity's view, and cannot switch fragment views during the activity's life cycle.

The second approach is more complex, but it is also the only way to control fragment at run time ( load, remove, replace ).

comparison of life cycle between fragment and activity

Fragment Life cycle Examples

Initial load: (divided into two parts)

Click the Home button (or access the phone), print the log as follows:

Re-entry into the program (or end of phone call), print the log as follows:

Click the Back button to exit the program, print the log as follows:

From the above log, we can see that the life cycle of fragment and activity is too similar. There are only a few new methods that are not available in the activity and need to be highlighted:

    • Onattach method: Called when fragment is associated with activity (gets the value of the activity's pass)
    • Oncreateview method: Called when creating a view for fragment (loading the layout) (drawing the UI layout to the current fragment, you can use the thread to update the UI)
    • Onactivitycreated method: This method is called when the OnCreate method in the activity finishes executing (indicating that the activity execution OnCreate method is complete)
    • Ondestroyview method: Called when layout in fragment is removed (represents fragment destroying associated UI layout)
    • Ondetach method: Called when fragment and activity are disassociate (detached activity)
third, the management of fragment return stack

Add fragment to the return stack:

Assuming we now have two fragment:fragment01 and Fragment02, we now jump from Fragment01 interface to Fragment02 and then press the back key to discover that the program is exiting directly instead of returning to 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 takes advantage of the knowledge of the return stack.

In fact, it's simple, Fragmenttransaction provides a addtobackstack () method that adds a transaction to the return stack.

Let's review the previously loaded fragment code, and on top of that, add a line of code that adds fragment to the return stack: (that is, the No. 07 line of code)

// Step One: Add an instance of Fragmenttransaction  =getfragmentmanager (). BeginTransaction (); // Step Two: Add the Fragment object with the Add () method New rightfragment (); Transaction.add (R.id.right, rightfragment); Transaction.addtobackstack (null);
// Step Three: Call the Commit () method to make the Fragmenttransaction instance change effective transaction.commit ();

We called Fragmenttransaction's Addtobackstack () method before the transaction was committed, and it could accept a name to describe the state of the return stack, which would normally pass in null.

Example

@Override Public voidOnClick (View v) {//TODO auto-generated Method StubTransaction = Manager.begintransaction (); 47Switch(V.getid ()) { Caser.id.button1:fragment01 fragment01=NewFragment01 (); Transaction.replace (R.id.right, fragment01,"Fragment01"); Transaction.addtobackstack ("fragment01");//added to the fallback stack for activity management.               Break;  Caser.id.button2:fragment02 fragment02=NewFragment02 (); Transaction.replace (R.id.right, fragment02,"Fragment02"); Transaction.addtobackstack ("fragment02");//added to the fallback stack for activity management.               Break;  Caser.id.button3:fragment03 fragment03=NewFragment03 (); Transaction.replace (R.id.right, fragment03,"Fragment03"); Transaction.addtobackstack ("fragment03");//added to the fallback stack for activity management.               Break;} transaction.commit (); }

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

Click the button to load the fragment01:

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

Note: If fragment01 is not pressed into the stack at the time of the replacement, it will be destroyed and will continue to execute the OnDestroy () and Ondetach () methods after the Ondestroyview () method is executed.

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

Press the back key again and the fragment01 is destroyed:

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

Android_fragment life cycle and return stack Backstack

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.