Life Cycle of Android Serial 9-Fragment

1. Fragmentation status and

1. Operation status

When a fragment is visible and her associated activity is running, the fragment is running.

2. Pause state

When one activity enters a pause state (because another activity that is not full of the screen is added to the top of the stack), the visible fragments associated with it enter a pause state.

3. Stop state

When an activity enters a stop state, the fragments associated with it enter a stop state.Alternatively, fragments can be removed from the activity by calling the remove(), replace() methods of the FragmentTransaction, but if the addToBackStack() method is called before the transaction is committed, the fragments will then go to a stop state.Generally speaking, fragments entering the stop state are completely invisible to users and may be recycled by the system.

4. Destruction status

Fragments are always dependent on activities, so when an activity is destroyed, the fragments associated with it go into a destroyed state.Alternatively, fragments can be removed from the activity by calling the remove(), replace() methods of the FragmentTransaction, but if the addToBackStack() method is not called before the transaction is committed, the fragments will then go to a stop state.

2. Additional callback methods

1.toAttach()

Called when an activity is associated with fragmentation

2.onCreateView()

Called when creating a view (loading layout) for fragmentation

3.onActivityCreated()

Ensure fragmentation-related attempts are called when they are removed

4.onDestroyView()

Called when the view associated with the fragment is removed.

5.onDetach()

Called when the fragment is disassociated from the activity

Full life cycle legend for fragments

 

package com.example.fragmenttest;

import android.app.Activity;

import android.app.Fragment;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

​

public class RightFragment extends Fragment {

  public static final String TAG = "RightFragment";

 

  @Override

  public void onAttach(Activity activity) {

    super.onAttach(activity);

    Log.d(TAG,"onAttach");

  }

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Log.d(TAG,"onCreate");

  }

 

  @Override

  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

    Log.d(TAG,"onCreateView");

    View view = inflater.inflate(R.layout.right_fragment,container,false);

    return view;

   

  }

 

  @Override

  public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    Log.d(TAG,"onActivity");

  }

 

  @Override

  public void onStart() {

    super.onStart();

    Log.d(TAG,"onStart");

   

  }

 

  @Override

  public void onResume() {

    super.onResume();

    Log.d(TAG,"onResume");

  }

 

  @Override

  public void onPause() {

    super.onPause();

    Log.d(TAG,"onPause");

   

  }

 

  @Override

  public void onStop() {

    super.onStop();

    Log.d(TAG,"onStop");

  }

 

  @Override

  public void onDestroy() {

    super.onDestroy();

    Log.d(TAG,"onDestroy");

​

  }

 

  @Override

  public void onDetach() {

    super.onDetach();

    Log.d(TAG,"onDetach");

  }

 

}

 

3. Source code:

1. Project Address

https://github.com/ruigege66/Android/tree/master/FragmentTest​

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Tags: Android Fragment github Big Data

Posted on Fri, 08 May 2020 12:41:50 -0400 by tmann