Fragment详解(二)--->生命周期详解

MainActivity如下:

package cc.testsimplefragment1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Demo描述:
 * Fragment生命周期
 *
 * 测试方法:
 * 在界面中从上至下点击各个按钮
 *
 * 参考资料:
 * 1 Android疯狂讲义(第二版)
 * 2 http://blog.163.com/supered_yang@126/blog/static/4126004120131710545228/
 * 3 http://blog.csdn.net/t12x3456/article/details/8104574
 *   Thank you very much
 *
 */
public class MainActivity extends Activity{
	private Button mStartActivityButton;
	private Button mAddFragmentButton;
	private Button mReplaceAndBackFragmentButton;
	private Button mReplaceFragmentButton;
	private Button mFinishButton;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init(){
		mStartActivityButton = (Button) findViewById(R.id.startActivityButton);
		mStartActivityButton.setOnClickListener(new ClickListenerImpl());

		mAddFragmentButton = (Button) findViewById(R.id.addFragmentButton);
		mAddFragmentButton.setOnClickListener(new ClickListenerImpl());

		mReplaceAndBackFragmentButton = (Button) findViewById(R.id.replaceAndBackFragmentButton);
		mReplaceAndBackFragmentButton.setOnClickListener(new ClickListenerImpl());

		mReplaceFragmentButton = (Button) findViewById(R.id.replaceFragmentButton);
		mReplaceFragmentButton.setOnClickListener(new ClickListenerImpl());

		mFinishButton = (Button) findViewById(R.id.finishButton);
		mFinishButton.setOnClickListener(new ClickListenerImpl());
	}

	private class ClickListenerImpl implements OnClickListener{
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.startActivityButton:
				Intent intent = new Intent(MainActivity.this, DialogStyleActivity.class);
				startActivity(intent);
				break;
			case R.id.addFragmentButton:
				TestLifecycleFragment testLifecycleFragment = new TestLifecycleFragment();
				getFragmentManager()
				.beginTransaction()
				.add(R.id.linearLayoutContainer, testLifecycleFragment)
				.commit();
				break;
			case R.id.replaceAndBackFragmentButton:
				AnotherFragment anotherFragment1 = new AnotherFragment();
				getFragmentManager()
				.beginTransaction()
				.replace(R.id.linearLayoutContainer, anotherFragment1)
				.addToBackStack("test")
				.commit();
				break;
			case R.id.replaceFragmentButton:
				AnotherFragment anotherFragment2 = new AnotherFragment();
				getFragmentManager()
				.beginTransaction()
				.replace(R.id.linearLayoutContainer, anotherFragment2)
				.commit();
				break;
			case R.id.finishButton:
				finish();
				break;
			default:
				break;
			}

		}

	}
}

TestLifecycleFragment如下:

package cc.testsimplefragment1;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TestLifecycleFragment extends Fragment {
	final String TAG = "log";
	/**
	 * 该Fragment被添加到Activity时调用.
	 * 只会被调用一次
	 */
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.d(TAG, "-------onAttach------");
	}

	/**
	 * 创建该Fragment时调用.
	 * 只会被调用一次
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "-------onCreate------");
	}

	/**
	 * 每次创建和绘制该Fragment的View组件时调用.
	 * Fragment会显示该方法返回的View
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle data) {
		Log.d(TAG, "-------onCreateView------");
		TextView tv = new TextView(getActivity());
		tv.setGravity(Gravity.CENTER_HORIZONTAL);
		tv.setText("这是一个用于测试的Fragment");
		tv.setTextSize(40);
		return tv;
	}

	/**
	 * 当Fragment所在的Activity被启动完成后
	 * 调用该方法
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "-------onActivityCreated------");
	}

	/**
	 * 启动Fragment时候调用该方法
	 */
	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "-------onStart------");
	}

	/**
	 * 恢复Fragment时候调用该方法.
	 * onStart()方法后一定会调用该onResume()方法
	 */
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "-------onResume------");
	}

    /**
     * 暂停Fragment时候调用该方法
     */
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "-------onPause------");
	}

	/**
     * 停止Fragment时候调用该方法
     */
	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "-------onStop------");
	}

	/**
     * 销毁该Fragment所包含的View调用该方法
     */
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "-------onDestroyView------");
	}

	/**
     * 销毁该Fragment时调用该方法
     * 该方法只会被调用一次
     */
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "-------onDestroy------");
	}

	/**
     * 将该Fragment从Activity中被删除,替换时调用该方法
     * 在onDestroy()方法后一定会调用该onDetach()方法.
     * 该方法只会被调用一次
     */
	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "-------onDetach------");
	}
}

AnotherFragment如下:

package cc.testsimplefragment1;

import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AnotherFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle data) {
		TextView textView = new TextView(getActivity());
		textView.setGravity(Gravity.CENTER_HORIZONTAL);
		textView.setText("另外一个Fragment");
		textView.setTextSize(40);
		return textView;
	}
}

DialogStyleActivity如下:

package cc.testsimplefragment1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
 * 备注说明:
 * 该Activity是对话框风格的Activity
 * 所以需要在配置文件中设置:
 * android:theme="@android:style/Theme.Holo.Dialog"
 *
 */
public class DialogStyleActivity extends Activity{
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		TextView textView = new TextView(this);
		textView.setText("对话框风格的Activity");
		setContentView(textView);
	}
}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayoutContainer"
        android:layout_width="wrap_content"
        android:layout_height="160dp" >
    </LinearLayout>

    <Button
        android:id="@+id/addFragmentButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="加载目标Fragment" />

    <Button
        android:id="@+id/startActivityButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="启动对话框风格的Activity" />

    <Button
        android:id="@+id/replaceAndBackFragmentButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="替换目标Fragment,并加入Back栈" />

    <Button
        android:id="@+id/replaceFragmentButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="替换目标Fragment" />

    <Button
        android:id="@+id/finishButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="退出" />

</LinearLayout>

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="cc.testsimplefragment1"
	android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk
		android:minSdkVersion="11"
		android:targetSdkVersion="17" />
	<application
		android:icon="@drawable/ic_launcher"
		android:label="@string/app_name">
		<activity
			android:name=".MainActivity"
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

		<activity
		    android:theme="@android:style/Theme.Holo.Dialog"
			android:name=".DialogStyleActivity"
			android:label="@string/app_name" />

	</application>
</manifest>

 

时间: 2024-08-02 07:56:48

Fragment详解(二)---&gt;生命周期详解的相关文章

php的生命周期详解

php的生命周期 在常见的webserver环境中, 你不能直接启动php解释器; 一般是启动apache或其他webserver, 由它们加载php处理需要处理的脚本(请求的.php文档). 一切都从sapi开始 尽管看起来有所不同, 但实际上CLI的行为和web方式一致. 在命令行中键入php命令将启动"命令行sapi", 它实际上就像一个设计用于服务单请求的迷你版webserver. 当脚本运行完成后, 这个迷你的php-webserver终止并返回控制给shell. 启动和终止

Android Activity生命周期详解_Android

Activity 的生命周期. 一.理解Activity Activity是Android程序的4大组件之一. Activity是Android程序的表示层.程序的每一个显示屏幕就是一个Activity. 学过WEB开发的同学,可以把Activity理解成网页中的一个JSP文件:或者你可以把它理解成一个Windows的窗口. 下面看一下Activity类的继承关系:    从这里可以看到Activity是Context类的子类,大家对此先有个印象.  二.理解Activity的生命周期 手机最重

Android开发之activity的生命周期详解_Android

本文实例讲述了Android activity的生命周期.分享给大家供大家参考,具体如下: activity类处于android.app包中,继承体系如下: 1.Java.lang.Object 2.android.content.Context 3.android.app.ApplicationContext 4.android.app.Activity activity是单独的,用于处理用户操作.几乎所有的activity都要和用户打交道,所以activity类创建了一个窗口,开发人员可以通

详解数据中心生命周期管理的要点

虽然我们已经进入云计算时代,企业的本地数据中心建设已经开始转向云端.但是对于,中大型企业或者特定行业客户来说,本地数据中心的建设仍是不可获取的工作.而对于数据中心的建设显然离不开生命周期管理,尤其是在数据中心数量不断增多且重要性日渐提升的今天,用传统理念看待数据中心已经不能满足时代发展的需求,数据中心不是一堆毫无生气的设备的堆砌,而是具备全生命周期,由供电.制冷.楼宇.安防和智能管理等子系统有机集成且能发挥更多作用的重要基础设施. 详解数据中心生命周期管理的要点 如何最大化数据中心在整个生命周期

Android Fragment的生命周期详解_Android

Fragments的生命周期        每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 详解Android Fragment之二:Fragment的创建和生命周期         创建片元(Creating a Fragment)        To create a fragment, you must create a subclass of Fragment (or an existing subclass of it).

Android编程中的四大基本组件与生命周期详解_Android

本文实例讲述了Android编程中的四大基本组件与生命周期.分享给大家供大家参考,具体如下: Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity : 应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应. Activity之间通过Intent进行通信.在Intent 的描述结构中,有两个最

Servlet生命周期详解

一.基本概念Servlet生命周期分为三个阶段 1.初始化阶段                调用init()方法 2.响应客户请求阶段     调用service()方法 3.终止阶段                   调用destroy()方法 二.详解1.初始化阶段在下列时刻Servlet容器装载Servlet: ①Servlet容器启动时自动装载某些Servlet,实现它只需要在web.XML文件中的之间添加代码:<load-on-startup>1</load-on-star

ASP.NET深入浅出系列2-页面生命周期详解

上个系列中介绍了页面生命周期的整体流程,可能有些读者还想更进一步了解整个生命周期的细节,限于篇幅我不可能讲到所有细节,也没必要,这里仅举几个例子,大家可以通过这几个例子学习一下页面生命周期的研究方式. Control类中有如下事件 // 当服务器控件绑定到数据源时发生. public event EventHandler DataBinding; // 当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段. public event EventHandler D

asp.net页面生命周期详解_实用技巧

Asp.net是微软.Net战略的一个组成部分.它相对以前的Asp有了很大的发展,引入了许多的新机制.本文就Asp.net页面的生命周期向大家做一个初步的介绍,以期能起到指导大家更好.更灵活地操纵Asp.net的作用.当一个获取网页的请求(可能是通过用户提交完成的,也可能是通过超链接完成的)被发送到Web服务器后,这个页面就会接着运行从创建到处理完成的一系列事件.在我们试图建立Asp.net页面的时候,这个执行周期是不必去考虑的,那样只会自讨苦吃.然而,如果被正确的操纵,一个页面的执行周期将是一