Android's fragment life cycle resolution

Source: Internet
Author: User

In the previous article on the basic application of fragment, let's show you what fragment life cycle is like. For basic application of fragemnt, see http://blog.csdn.net/jiapeng2b/article/details/46919859.

First, let's look at the life cycle of fragment first.

Compared to the activity life cycle

Activity directly affects the life cycle of the fragment it contains, so a call to the activity's life cycle method will also produce a call to the same method as the fragment. For example: When the activity's OnPause () method is called, all of the fragment's OnPause () methods it contains are called.

Fragment has several life-cycle callback methods than the activity, and these additional methods are created to interact with the activity, as follows:

Onattach ()

当fragment被加入到activity时调用(在这个方法中可以获得所在的activity)。

Oncreateview ()

当activity要得到fragment的layout时,调用此方法,fragment在其中创建自己的layout(界面)。

Onactivitycreated ()

当activity的onCreated()方法返回后调用此方法。

Ondestroyview ()

当fragment的layout被销毁时被调用。

Ondetach ()

当fragment被从activity中删掉时被调用。

Once the activity enters the resumed state (that is, the running state), you are free to add and remove fragment. Therefore, the life cycle of fragment can be operated independently only when the activity is in the resumed state, and other times depend on the life cycle change of the activity.

Second, the test code (here is a simple implementation, print out)

 PackageCom.jp.fragment;Importandroid.app.Activity;ImportAndroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportCOM.EXAMPLE.FRAMENTDEMO.R; Public  class fragmentdemo extends Fragment {    /** * Called when fragment is added to the activity (where the activity is available in this method). */     Public void Onattach(Activity activity) {Super. Onattach (activity); System.out.println ("Fragment ———— Onattach ()"); }/** * When the activity wants to get fragment layout, call this method, fragment in which to create their own layout (interface). */     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {System.out.println ("Fragment ———— ()");returnInflater.inflate (R.layout.title_frament, container,false); } Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); System.out.println ("Fragment ———— onCreate ()"); }/** * This method is called when the activity's oncreated () method returns. */     Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate); System.out.println ("Fragment ———— onactivitycreated ()"); }/**     *      */     Public void OnStart() {Super. OnStart (); System.out.println ("Fragment ———— OnStart ()"); }/**     *      */     Public void Onresume() {Super. Onresume (); System.out.println ("Fragment ———— onresume ()"); } Public void OnPause() {Super. OnPause (); System.out.println ("Fragment ———— onPause ()"); } Public void OnStop() {Super. OnStop (); System.out.println ("Fragment ———— onStop ()"); }/** * is called when the layout of fragment is destroyed. */     Public void Ondestroyview() {Super. Ondestroyview (); System.out.println ("Fragment ———— Ondestroyview ()"); } Public void OnDestroy() {Super. OnDestroy (); System.out.println ("Fragment ———— OnDestroy ()"); }/** * is called when fragment is deleted from activity. */     Public void Ondetach() {Super. Ondetach (); System.out.println ("Fragment ———— Ondetach ()"); }}

Three, the scene demonstration

场景演示 : 切换到该Fragment

切换到其他Fragment:

切换回本身的Fragment:

回到桌面:

回到应用:

退出应用:

Simply introduced here, I hope to help you ~ ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android's fragment life cycle resolution

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.