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>