布局中使用fragment标签,Activity与Fragment的生命周期

Activity的生命周期

这里写图片描述

Fragment的生命周期

这里写图片描述

Fragment与Activity生命周期的联系

这里写图片描述

例子

MainActivity代码:

package com.xqq.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("Activity  onCreate");
    }

    public void click(View view){
        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        System.out.println("Activity  onStart");
        super.onStart();
    }

    @Override
    protected void onResume() {
        System.out.println("Activity  onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        System.out.println("Activity  onPause");
        super.onPause();
    }
    @Override
    protected void onRestart() {
        System.out.println("Activity  onRestart");
        super.onRestart();
    }
    @Override
    protected void onStop() {
        System.out.println("Activity  onStop");
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        System.out.println("Activity  onDestroy");
        super.onDestroy();
    }
}

Fragment代码:

package com.xqq.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Fragment1 extends Fragment{
    @Override
    public void onAttach(Activity activity) {
        System.out.println("Fragment1  onAttach");
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.out.println("Fragment1  onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        System.out.println("Fragment1  onCreateView");
        return inflater.inflate(R.layout.fragment, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        System.out.println("Fragment1  onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        System.out.println("Fragment1  onStart");
        super.onStart();
    }
    @Override
    public void onResume() {
        System.out.println("Fragment1  onResume");
        super.onResume();
    }

    @Override
    public void onPause() {
        System.out.println("Fragment1  onPause");
        super.onPause();
    }
    @Override
    public void onStop() {
        System.out.println("Fragment1  onStop");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        System.out.println("Fragment1  onDestroyView");
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        System.out.println("Fragment1  onDestroy");
        super.onDestroy();
    }
    @Override
    public void onDetach() {
        System.out.println("Fragment1  onDetach");
        super.onDetach();
    }
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xqq.fragment.MainActivity" 
    android:orientation="vertical">

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"/>

    <fragment
        android:name="com.xqq.fragment.Fragment1"
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

运行结果与分析:

onCreate:第一次打开应用程序,Activity被创建。
onStart:当Activity变成用户可见时调用的方法。
onRestart:重新用户可见时调用的方法,在onStart()方法调用之后调用。
onResume:当界面获得焦点时调用的方法
onPause:当界面失去焦点时调用的方法
onStop():当Activity变成用户不可见时调用的方法。
onDestroy:Activity销毁时调用的方法

//启动应用程序进入MainActivity
07-19 07:41:54.157: I/System.out(732): Fragment1  onAttach
07-19 07:41:54.157: I/System.out(732): Fragment1  onCreate
07-19 07:41:54.157: I/System.out(732): Fragment1  onCreateView
07-19 07:41:54.157: I/System.out(732): Fragment1  onViewCreated
07-19 07:41:54.193: I/System.out(732): Activity  onCreate
07-19 07:41:54.193: I/System.out(732): Activity  onStart
07-19 07:41:54.193: I/System.out(732): Fragment1  onActivityCreated
07-19 07:41:54.197: I/System.out(732): Fragment1  onStart
07-19 07:41:54.197: I/System.out(732): Activity  onResume
07-19 07:41:54.197: I/System.out(732): Fragment1  onResume

onCreate里 使用supportManager添加Fragment
02-08 14:07:28.555 16387-16387/com.zxl.practice I/System.out: Activity  onCreate
02-08 14:07:28.556 16387-16387/com.zxl.practice I/System.out: Activity  onStart
02-08 14:07:28.556 16387-16387/com.zxl.practice I/System.out: Fragment2  onAttach
02-08 14:07:28.556 16387-16387/com.zxl.practice I/System.out: Fragment2  onCreate
02-08 14:07:28.556 16387-16387/com.zxl.practice I/System.out: Fragment2  onCreateView
02-08 14:07:28.557 16387-16387/com.zxl.practice I/System.out: Fragment2  onViewCreated
02-08 14:07:28.557 16387-16387/com.zxl.practice I/System.out: Fragment2  onActivityCreated
02-08 14:07:28.557 16387-16387/com.zxl.practice I/System.out: Fragment2  onStart
02-08 14:07:28.558 16387-16387/com.zxl.practice I/System.out: Activity  onResume
02-08 14:07:28.558 16387-16387/com.zxl.practice I/System.out: Fragment2  onResume
Fragment1  xml代码里                Fragment2 onCreate里动态加入
02-08 14:18:55.823 17451-17451/com.zxl.practice I/System.out: Fragment1  onAttach
02-08 14:18:55.823 17451-17451/com.zxl.practice I/System.out: Fragment1  onCreate
02-08 14:18:55.823 17451-17451/com.zxl.practice I/System.out: Fragment1  onCreateView
02-08 14:18:55.825 17451-17451/com.zxl.practice I/System.out: Fragment1  onViewCreated
02-08 14:18:55.827 17451-17451/com.zxl.practice I/System.out: Fragment1  onActivityCreated
02-08 14:18:55.829 17451-17451/com.zxl.practice I/System.out: Fragment2  onAttach
02-08 14:18:55.830 17451-17451/com.zxl.practice I/System.out: Fragment2  onCreate
02-08 14:18:55.830 17451-17451/com.zxl.practice I/System.out: Fragment2  onCreateView
02-08 14:18:55.831 17451-17451/com.zxl.practice I/System.out: Fragment2  onViewCreated
02-08 14:18:55.831 17451-17451/com.zxl.practice I/System.out: Fragment2  onActivityCreated
02-08 14:18:55.831 17451-17451/com.zxl.practice I/System.out: Fragment1  onStart
02-08 14:18:55.831 17451-17451/com.zxl.practice I/System.out: Fragment2  onStart
02-08 14:18:55.832 17451-17451/com.zxl.practice I/System.out: Fragment1  onResume
02-08 14:18:55.832 17451-17451/com.zxl.practice I/System.out: Fragment2  onResume
加上Activity周期
02-08 14:21:08.938 17876-17876/com.zxl.practice I/System.out: Fragment1  onAttach
02-08 14:21:08.938 17876-17876/com.zxl.practice I/System.out: Fragment1  onCreate
02-08 14:21:08.938 17876-17876/com.zxl.practice I/System.out: Fragment1  onCreateView
02-08 14:21:08.940 17876-17876/com.zxl.practice I/System.out: Fragment1  onViewCreated
02-08 14:21:08.941 17876-17876/com.zxl.practice I/System.out: Activity  onCreate
02-08 14:21:08.942 17876-17876/com.zxl.practice I/System.out: Activity  onStart
02-08 14:21:08.942 17876-17876/com.zxl.practice I/System.out: Fragment1  onActivityCreated
02-08 14:21:08.944 17876-17876/com.zxl.practice I/System.out: Fragment2  onAttach
02-08 14:21:08.944 17876-17876/com.zxl.practice I/System.out: Fragment2  onCreate
02-08 14:21:08.945 17876-17876/com.zxl.practice I/System.out: Fragment2  onCreateView
02-08 14:21:08.946 17876-17876/com.zxl.practice I/System.out: Fragment2  onViewCreated
02-08 14:21:08.946 17876-17876/com.zxl.practice I/System.out: Fragment2  onActivityCreated
02-08 14:21:08.946 17876-17876/com.zxl.practice I/System.out: Fragment1  onStart
02-08 14:21:08.946 17876-17876/com.zxl.practice I/System.out: Fragment2  onStart
02-08 14:21:08.947 17876-17876/com.zxl.practice I/System.out: Activity  onResume
02-08 14:21:08.947 17876-17876/com.zxl.practice I/System.out: Fragment1  onResume
02-08 14:21:08.947 17876-17876/com.zxl.practice I/System.out: Fragment2  onResume

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值