Activity和Fragment生命周期

文章目录

          • Activity的生命周期
          • Activity和Fragment生命周期对比
          • 代码
          • 测试Log
          • 总结

Activity的生命周期

Activity的生命周期中有7个方法,Fragment生命周期中有11个方法
Activity和Fragment生命周期_第1张图片

Activity和Fragment生命周期对比

Activity生命周期中除了以下6个方法外还有一个onRestart()方法,该方法在该Activity从不可见(仍存在)到重新可见时调用
Activity和Fragment生命周期_第2张图片

代码
package com.example.asus.animport;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("info","Activity--onCreate()");
        setContentView(R.layout.activity_main);

        android.app.Fragment tFragment = new TFragment();
        getFragmentManager().beginTransaction().add(R.id.FLayout,tFragment).commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("info","Activity--onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("info","Activity--onResumed()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("info","Activity--onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("info","Activity--onStop()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("info","Activity--onRestart()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("info","Activity--onDestroy()");
    }

    public static class TFragment extends android.app.Fragment{

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.e("info","Fragment--onAttach()");
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e("info","Fragment--onCreate()");
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Log.e("info","Fragment--onCreateView()");
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.e("info","Fragment--onActivityCreated()");
        }

        @Override
        public void onStart() {
            super.onStart();
            Log.e("info","Fragment--onStart()");
        }

        @Override
        public void onResume() {
            super.onResume();
            Log.e("info","Fragment--onResume()");
        }

        @Override
        public void onPause() {
            super.onPause();
            Log.e("info","Fragment--onPause()");
        }

        @Override
        public void onStop() {
            super.onStop();
            Log.e("info","Fragment--onStop()");
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.e("info","Fragment--onDestroyView()");
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e("info","Fragment--onDestroy()");
        }

        @Override
        public void onDetach() {
            super.onDetach();
            Log.e("info","Fragment--onDetach()");
        }
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.asus.animport.MainActivity">

    <FrameLayout
        android:id="@+id/FLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    FrameLayout>
LinearLayout>
测试Log
  1. 运行程序:
    Activity和Fragment生命周期_第3张图片
  2. 切换到其他应用:
    image
  3. 切换回本应用:
    Activity和Fragment生命周期_第4张图片
  4. 退出应用:
    Activity和Fragment生命周期_第5张图片
总结

特别注意由于Fragment依赖Activity的存在而存在,故而在创建Activity生命周期中的方法均先于Fragment生命周期中的方法;相反,在销毁时,先执行Fragment生命周期中的方法,再执行Activity生命周期中的方法。
值得一提的是,Fragment生命周期中的方法onAttact(Context context)在执行该程序中并没有执行,查阅资料发现在API低于23版本时不会执行该方法,而是执行onAttach(Activity activity)方法。参考:http://blog.csdn.net/cadi2011/article/details/53314055

本文转自:https://www.cnblogs.com/LangZXG/p/6501839.html

你可能感兴趣的:(interview,Android)