編輯:關於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(); } }
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(); } }
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---->onResumeactivity被擋住生命周期方法
一、 Android分發機制概述: Android如此受歡迎,就在於其優秀的交互性,這其中,Android優秀的事件分發機制功不可沒。那麼,作為一個優秀的程序員,要想做
本文實例為大家分享了Android實現單項、多項選擇操作的相關代碼,供大家參考,具體內容如下1、單項選擇1.1.布局<?xml version=1.0 en
最近事情很忙,一個新項目趕著出來,但是很多功能都要重新做,一直在編寫代碼、Debug。今天因為一個新程序要使用Fragment來做,雖然以前也使用過Fragment,不過
最近要面試了 沒 時間玩了.看了下安卓icp ,說實話一直沒過aidl這東東 發現網上帖子好多 下載的卻好少 .好吧自己玩了一個 留下做個備注吧. 服務端輸入文