Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android fragment生命周期

android fragment生命周期

編輯:關於Android編程

fragment生命周期和activity生命周期很像,現在我們通過例子來看看在不同的場景會調用生命周期那些方法,創建一個android項目 lifefragment

MainActivity.java

package com.example.fragmentlifecycle;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class MainActivity extends FragmentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.container, new LifeFragment());
		ft.commit();
		Log.e("生命周期","activity---->onCreate");
	}

	@Override
	protected void onDestroy() {
		Log.e("生命周期","activity---->onCreate");
		super.onDestroy();
		
	}

	@Override
	protected void onPause() {
		Log.e("生命周期","activity---->onCreate");
		super.onPause();
	}

	@Override
	protected void onResume() {
		Log.e("生命周期","activity---->onCreate");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.e("生命周期","activity---->onCreate");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.e("生命周期","activity---->onCreate");
		super.onStop();
	}
	
	@Override
	protected void onRestart() {
		Log.e("生命周期","activity---->onCreate");
		super.onRestart();
	}
}

LifeFragment.java

package com.example.fragmentlifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LifeFragment extends Fragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.e("生命周期","Fragment---->onActivityCreated");
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.e("生命周期","Fragment---->onCreateView");
		return inflater.inflate(R.layout.lifefragment, null);
	}

	@Override
	public void onAttach(Activity activity) {
		Log.e("生命周期","Fragment---->onAttach");
		Log.e("LifeFragment","activity="+activity);
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.e("生命周期","Fragment---->onCreate");
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		Log.e("生命周期","Fragment---->onDestroy");
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		Log.e("生命周期","Fragment---->onDetach");
		super.onDetach();
	}

	@Override
	public void onPause() {
		Log.e("生命周期","Fragment---->onPause");
		super.onPause();
	}

	@Override
	public void onResume() {
		Log.e("生命周期","Fragment---->onResume");
		super.onResume();
	}

	@Override
	public void onStart() {
		Log.e("生命周期","Fragment---->onStart");
		super.onStart();
	}

	@Override
	public void onStop() {
		Log.e("生命周期","Fragment---->onStop");
		super.onStop();
	}
	
	@Override
	public void onDestroyView() {
		Log.e("生命周期","Fragment---->onDestroyView");
		super.onDestroyView();
	}
}

當第一次運行的時候log信息:

11-03 13:35:37.693: E/生命周期(14098): activity---->onCreate
11-03 13:35:37.693: E/生命周期(14098): activity---->onStart
11-03 13:35:37.693: E/生命周期(14098): Fragment---->onAttach
11-03 13:35:37.693: E/生命周期(14098): Fragment---->onCreate
11-03 13:35:37.694: E/生命周期(14098): Fragment---->onCreateView
11-03 13:35:37.697: E/生命周期(14098): Fragment---->onActivityCreated
11-03 13:35:37.697: E/生命周期(14098): Fragment---->onStart
11-03 13:35:37.698: E/生命周期(14098): activity---->onResume
11-03 13:35:37.698: E/生命周期(14098): Fragment---->onResume


activity被擋住生命周期方法




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved