A case study on the fragment lifecycle of Android application writing _android

Source: Internet
Author: User

Managing fragment life cycles is somewhat like managing the lifecycle of an activity. Fragment can survive in three different states:

Resumed:

The fragment is in a running activity and is visible.

Paused:

Another activity is at the top, but the activity of fragment is not fully covered (the top-level activity is translucent or does not occupy the entire screen).

stoped:

Fragment is not visible. It may be that its activity is in a stoped state or that the fragment is deleted and added to the back stack. The fragment of this state still exists in memory.

Similarly to activity, you can keep the state of fragment in a bundle, which is needed when the activity is recreated. You can save the state in the Onsaveinstancestate () method and restore it in OnCreate () or Oncreateview () or onactivitycreated ().

The biggest difference between the fragment and the activity life cycle is the process that is stored in the back stack. An activity is automatically pushed into the stop stack by the system at stop, and the stack is managed by the system, and fragment is a back stack that is managed by the activity, and only after you delete the fragment and explicitly call Addtobackstack () Method before being pressed into it.

However, the lifecycle of managing fragment is extremely similar to the life cycle of managing activity. What you need to think about is how the life cycle of the activity affects the life cycle of the fragment.


Here's a fragment life cycle chart that you should see a lot of:

But recently in the Write PageManager (Management page jump), found that the page, before the page after the Ondestoryview directly ondestory, back again OnCreate, if the way of hide and show, do not take the life cycle, Read the Apidemo code, find out why, tidy up.
There are two ways to switch fragment, one is add new, and the old hide, the other is replace.
The old fragment is Fragment1, and the new is Fragment2, ignoring the non-critical lifecycle.

When you use the Add method to toggle:
Load into Fragment1

Fragment1 onCreate
Fragment1 oncreateview
Fragment1 onStart Fragment1 onresume

Cut to Fragment2 with the following code:

Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Ft.hide (Fragment1);
Ft.add (R.id.simple_fragment, Fragment2);
Ft.settransition (Fragmenttransaction.transit_fragment_open);
Ft.commit ();

Fragment1 does not take any life cycle, but will adjust the Onhiddenchanged method

Fragment2 onCreate
Fragment2 oncreateview
Fragment2 onStart Fragment2 onresume

Back to Fragment1,remove Fragment2:

Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Ft.remove (Fragment2);
Ft.show (Fragment1);
Ft.settransition (Fragmenttransaction.transit_fragment_open);
Ft.commit ();

Fragment1 or not take any life cycle, adjust the Onhiddenchanged method

Fragment2 onpause
Fragment2 onStop
Fragment2 ondestoryview Fragment2 ondestory

In this way, fragment will not walk Ondestoryview when hiding, so the display will not go oncreateview, all view has been kept in memory.
Use the Replace method:
The load Fragment1 lifecycle is the same as above:

Fragment1 onCreate
Fragment1 oncreateview
Fragment1 onStart Fragment1 onresume

Cut to Fragment2:

Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Ft.replace (R.id.simple_fragment, Fragment2);
Ft.settransition (Fragmenttransaction.transit_fragment_open);
Ft.commit ();

This time, the Fragment1 's life cycle.

Fragment1 onpause
Fragment1 onStop
Fragment1 ondestoryview
Fragment1 ondestory
Fragment2 onCreate
Fragment2 Oncreateview
Fragment2 OnStart
Fragment2 onresume

True print out may be Fragment1 and Fragment2 mixed together, can see, Fragment1 walked ondestory, was completely recycled!
and cut back to Fragment1.

Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
Ft.replace (R.id.simple_fragment, Fragment1);
Ft.settransition (Fragmenttransaction.transit_fragment_open);
Ft.commit ();

Fragment1 onCreate
Fragment1 oncreateview
Fragment1 onStart
Fragment1 onresume
Fragment2 onpause
Fragment2 onStop
Fragment2 Ondestoryview
Fragment2 ondestory

Fragment1 because it has been recycled and gone oncreate,fragment2 is recycled.

Neither of these methods is clearly satisfying my needs and is different from the life cycle diagram. Because I need something like register and unregister broadcastreceiver when the user sees/sees the fragment (Onhiddenchanged can also be implemented, but the first time you load the display, And do not go onhiddenchanged method when destroying), also do not want the user to go back to the previous fragment to re-create entire fragment, because this consumes resources.

Looking at the Apidemo, the discovery is also using the Replace method, but, I am missing a line:

Ft.addtobackstack (NULL);

Add this line in replace, you can put the original fragment into the stack, go Ondestoryview method, but will not ondestory, return, direct Oncreateview, no longer oncreate.
Returns a direct call to the Popbackstack () method:

Getfragmentmanager (). Popbackstack ();

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.