




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、onAestarlf)onPausef)onStop()onD&stroyOActivftyis shutdownThe activitycorner torheThe activitycomes to theforegroundActivitystarts此文于2010-08-03被推薦到(7?用血啊慮no longer visibQ我在前面也曾經(jīng)講過這方面API ,過于籠統(tǒng),相信大家看兩分鐘徹底讓你明白Android Activity生命周期(圖文)!收藏CSDN首頁如何被推薦?大家好,今天給大家詳解一下Android中Activity的生命周期,的內(nèi)容,但是像網(wǎng)上大多數(shù)文章一樣,
2、基本都是翻譯Android了,會(huì)有一點(diǎn)點(diǎn)的幫助,但是還不能完全吃透,所以我今天特意在重新總結(jié)一下首先看一下Android api中所提供的Activity生命周期圖(不明白的,可以看完整篇文章 回頭看一下這個(gè)圖,你會(huì)明白的):User navigatesback to th&f Process is ( killed JJrOtherapiplicationsonCrtiaieOonSiano1ron ReswmefAnotheract ,in frontrivitycomes theactivityActivity isrunningActivity其實(shí)是繼承了Application
3、context這個(gè)類,我們可以重寫以下方法,如下代碼 view plaincopy toclipboardprint?01.public class Activity extends Applicationcontext (02. protected void onCreate(Bundle savedInstanceState);03.04.protected void onStart();05.06.protected void onRestart();07.08.protected void onResume();09.10.protected void onPause();11.11.
4、protected void onStop();13.12.protected void onDestroy();13.public class Activity extends ApplicationContext (protected void onCreate(Bundle savedInstanceState);protected void onStart();protected void onRestart();protected void onResume();protected void onPause();protected void onStop();protected vo
5、id onDestroy();為了便于大家更好的理解,我簡單的寫了一個(gè)Demo,不明白Activity周期的朋友們,可以親手實(shí)踐一下,大家按照我的步驟來。第一步:新建一個(gè)Android工程,我這里命名為ActivityDemo.第二步修改ActivityDemo.java(我這里重新寫了以上的七種方法,主要用Log打印),代碼如下:view plaincopy to clipboardprint?01.package com.tutor.activitydemo;02.import android.app.Activity;03.import android.os.Bundle;04.impo
6、rt android.util.Log;05.public class ActivityDemo extends Activity 06.07. private static final String TAG = ActivityDemo;08.09. public void onCreate(Bundle savedInstanceState) 10.super.onCreate(savedInstanceState);11.setContentView(R.layout.main);12.12.Log.e(TAG, start onCreate- );13.15.14.Override15
7、.protected voidonStart() 16.super.onStart();17.Log.e(TAG, start onStart-);18.21.19.Otected voidonRestart() 21.super.onRestart();22.Log.e(TAG, start onRestart);23.27.24.Otected voidonResume() 26.super.onResume();27.Log.e(TAG, start onResume);28.33.29.Otected voi
8、d onPause() 31.super.onPause();32.Log.e(TAG, start onPause);33.39.34.Otected void onStop() 36.super.onStop();37.Log.e(TAG, start onStop);38.45.39.Otected void onDestroy() (41.super.onDestroy();42.Log.e(TAG, start onDestroy- );43.51.44.package com.tutor.activitydemo;import a
9、ndroid.app.Activity;import android.os.Bundle;import android.util.Log;public class ActivityDemo extends Activity (private static final String TAG = ActivityDemo;public void onCreate(Bundle savedInstanceState) ( super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.e(TAG, start onCreate
10、-);Overrideprotected void onStart() (super.onStart();Log.e(TAG, start onStart-);Overrideprotected void onRestart() (super.onRestart();Log.e(TAG, start onRestart- );Overrideprotected void onResume() (super.onResume();Log.e(TAG, start onResume-);Overrideprotected void onPause() ( super.onPause();Log.e
11、(TAG, start onPause-);Overrideprotected void onStop() super.onStop();Log.e(TAG, start onStop-);Overrideprotected void onDestroy() super.onDestroy();Log.e(TAG, start onDestroy-);第三步:運(yùn)行上述工程,效果圖如下(沒什么特別的):IS8ffi1:00PM|Activity DemoHello Worfd, ActivityDemo!核心在Logcat視窗里,如果你還不會(huì)用Logcat你可以看一下我的這篇文章Log圖文詳解(
12、Log.v,Log.d,Log.i,Log.w,Log.e), 我們打 開應(yīng)用 時(shí)先后 執(zhí)行了onCreate()-onStart()-onResume三個(gè)方法,看一下LogCat視窗如下:ActivityDemoHello World. ActivityDemo!BACK鍵:當(dāng)我們按BACK鍵時(shí),我們這個(gè)應(yīng)用程序?qū)⒔Y(jié)束,這時(shí)候我們將先后調(diào)用onPause()-onStop()-onDestory()三個(gè)方法,如下圖所示:當(dāng)我們打開應(yīng)用程序時(shí),比如瀏覽器,我正在瀏覽NBA新聞,看到一半時(shí),我突然想聽歌,這時(shí)候我們會(huì)選擇按HOME鍵,然后去打開音樂應(yīng)用程序,而當(dāng)我們按HOME的時(shí)候,Activi
13、ty先后執(zhí)行了onPause()-onStop()這兩個(gè)方法,這時(shí)候應(yīng)用程序并沒有銷毀。如下圖 所示:HOME鍵:而當(dāng)我們?cè)俅螁?dòng)ActivityDemo應(yīng)用程序時(shí),則先后分別執(zhí)行了onRestart()-onStart()-onResume()三個(gè)方法,如下圖所示:這里我們會(huì)引出一個(gè)問題,當(dāng)我們按HOME鍵,然后再進(jìn)入ActivityDemo應(yīng)用時(shí),我們的應(yīng)用的狀態(tài)應(yīng)該是和按HOME鍵之前的狀態(tài)是一樣的,同樣為了方便理解,在這里我將ActivityDemo的代碼作一些修改,就是增加一個(gè)EditText。第四步 修改main.xml布局文件(增加了一個(gè)EditText),代碼如下:view p
14、laincopy to clipboardprint?01.02.07.12.17.第五步:然后其他不變,運(yùn)行ActivityDemo程序,在EditText里輸入如Frankie字符串(如下圖:)這時(shí)候,大家可以按一下HOME鍵,然后再次啟動(dòng)ActivityDemo應(yīng)用程序,這時(shí)候EditText里并沒有我們輸入的Frankie字樣,如下圖:這顯然不能稱得一個(gè)合格的應(yīng)用程序,所以我們需要在Activity幾個(gè)方法里自己實(shí)現(xiàn),如下第六步所示:第六步修改 ActivityDemo.java 代碼如下:view plaincopy to clipboardprint?01.package com.
15、tutor.activitydemo;02.import android.app.Activity;03.import android.os.Bundle;04.import android.util.Log;05.import android.widget.EditText;06.public class ActivityDemo extends Activity (07.08. private static final String TAG = ActivityDemo;09.private EditText mEditText;10.定義一個(gè)String類型用來存取我們EditText輸
16、入的值11.private String mString;12.public void onCreate(Bundle savedInstanceState) (13.super.onCreate(savedInstanceState);14.setContentView(R.layout.main);15.mEditText = (EditText)findV iewById(R.id.editText);16.Log.e(TAG, start onCreate- );17.18.18.Otected voidonStart() (20.super.onStart(
17、);21.Log.e(TAG, start onStart-);22.23.當(dāng)按HOME鍵時(shí),然后再次啟動(dòng)應(yīng)用時(shí),我們要恢復(fù)先前狀態(tài)24.Otected void onRestart() (26.super.onRestart();27.mEditText.setText(mString);28.Log.e(TAG, start onRestart-);29.31.30.Otected void onResume() (32.super.onResume();33.Log.e(TAG, start onResume- );34.37.35.當(dāng)
18、我們按HOME鍵時(shí),我在onPause方法里,將輸入的值賦給mString36.Otected void onPause() (38.super.onPause();39.mString = mEditText.getText().toString();40.Log.e(TAG, start onPause-);41.45.42.Otected void onStop() (44.super.onStop();45.Log.e(TAG, start onStop- );46.51.47.Otected void on
19、Destroy()(49.super.onDestroy();50.Log.e(TAG, start onDestroy-);51.57.52.package com.tutor.activitydemo;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.EditText;public class ActivityDemo extends Activity private static final String TAG = ActivityDemo
20、;private EditText mEditText;定義一個(gè)String類型用來存取我們EditText輸入的值private String mString;public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);mEditText = (EditText)findView ById(R.id.editText);Log.e(TAG, start onCreate-);Overrideprotected void onStart() super.onStart();Log.e(TAG, start onStart-);/當(dāng)按HOME鍵時(shí),然后再次啟動(dòng)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 25246-2025畜禽糞肥還田技術(shù)規(guī)范
- 2025年常德c1貨運(yùn)從業(yè)資格證考試內(nèi)容
- 兒童桌子采購合同范本
- 鄉(xiāng)鎮(zhèn)飯店轉(zhuǎn)讓合同范本
- 公司房租轉(zhuǎn)租合同范本
- 倉庫裝修合同范本版
- 上海廠房出售合同范本
- 茶器定制合同范本
- 中標(biāo)咨詢合同范本
- 農(nóng)村訂購混泥土合同范本
- 小學(xué)數(shù)學(xué)主題活動(dòng)設(shè)計(jì)一年級(jí)《歡樂購物街》
- 一年級(jí)口算天天練1(打印版)
- 垃圾發(fā)電廠汽機(jī)培訓(xùn)
- 村情要素模板
- 高考作文標(biāo)準(zhǔn)方格紙-A4-可直接打印
- 護(hù)理分級(jí)標(biāo)準(zhǔn)
- 水池維修改造方案
- 國網(wǎng)陜西省電力有限公司高校畢業(yè)生招聘考試試題及答案
- 非公司企業(yè)法人章程范本樣本
- 《高溫熔融金屬吊運(yùn)安全規(guī)程》(AQ7011-2018)
- 電機(jī)與拖動(dòng)(高職)全套教學(xué)課件
評(píng)論
0/150
提交評(píng)論