



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Activity的屬性之—launchMode
Activity的屬性之—launchMode在manifest清單文件中配置activity的時(shí)候,有一個(gè)屬性叫l(wèi)aunchMode<Activity
……android:launchMode=["multiple"|"singleTop"|
"singleTask"|"singleInstance"]
/>LaunchMode指定該activity的加載模式,如上所述加載模式有四種分別是standardsingleTopsingleTask
和singleInstance.為什么要指定activity的加載模式呢?因?yàn)锳ndroid系統(tǒng)對activity是通過棧來管理的,當(dāng)應(yīng)用啟動(dòng)的時(shí)候相應(yīng)的系統(tǒng)就會(huì)為這個(gè)應(yīng)用創(chuàng)建一個(gè)棧,然后啟動(dòng)應(yīng)用的mainactivity。就相當(dāng)于數(shù)據(jù)結(jié)構(gòu)中的出棧和入棧一樣的管理本應(yīng)用中的activity。而activity的加載模式就可以管理activity的實(shí)例化和加載方式,以及加載的順序關(guān)系等。
那么下面來介紹一下這四種加載模式:1、
standard這個(gè)模式是activity默認(rèn)啟動(dòng)模式,也就是說在不指定activity的launchMode的情況下就是按照這種模式啟動(dòng)的,這種模式下每次啟動(dòng)一個(gè)新的activity的時(shí)候都會(huì)為目標(biāo)activity創(chuàng)建一個(gè)新的實(shí)例,并將此實(shí)例添加到棧中(也就是在棧頂)(PS一般的那個(gè)錢顯示給用戶的activity都在棧頂)。
2、
singleTop在這個(gè)模式下的時(shí)候,在啟動(dòng)目標(biāo)的activity的時(shí)候,如果當(dāng)前應(yīng)用的棧中的棧頂已經(jīng)是目標(biāo)activity的實(shí)例的話那么就不會(huì)重新創(chuàng)建新的實(shí)例,而直接使用棧頂?shù)膶?shí)例。(如果被啟動(dòng)目標(biāo)沒有在棧頂,那么此時(shí)系統(tǒng)會(huì)重新創(chuàng)建一個(gè)該activity的實(shí)例并將它加載到棧頂,這時(shí)它和standard模式完全一樣)3、
singleTask這種模式下,如果當(dāng)前棧中有將要啟動(dòng)activity的實(shí)例那么直接調(diào)用這個(gè)實(shí)例,也就是在同一個(gè)棧中只有一個(gè)實(shí)例。使用
singleTop模式可以很好地解決重復(fù)創(chuàng)建棧頂活動(dòng)的問題,但是如果該activity并沒有處于棧頂?shù)奈恢?,還是可能會(huì)創(chuàng)建多個(gè)activity實(shí)例的。那么有沒有什么辦法可以讓某個(gè)activity在整個(gè)應(yīng)用程序的上下文中只存在一個(gè)實(shí)例呢?這就要借助singleTask模式來實(shí)現(xiàn)了。當(dāng)activity的啟動(dòng)模式指定為
singleTask,每次啟動(dòng)該activity時(shí)系統(tǒng)首先會(huì)在返回棧中檢查是否存在該activity的實(shí)例,如果發(fā)現(xiàn)已經(jīng)存在則直接使用該實(shí)例,并把在這個(gè)activity之上的所有activity統(tǒng)統(tǒng)出棧,如果沒有發(fā)現(xiàn)就會(huì)創(chuàng)建一個(gè)新的activity實(shí)例4、
singleInstancesingleInstance模式應(yīng)該算是四種啟動(dòng)模式中最特殊也最復(fù)雜的一個(gè)了,你也需要多花點(diǎn)功夫來理解這個(gè)模式。不同于以上三種啟動(dòng)模式,指定為
singleInstance模式的activity會(huì)啟用一個(gè)新的返回棧來管理這個(gè)activity(其實(shí)如果singleTask模式指定了不同的
taskAffinity,也會(huì)啟動(dòng)一個(gè)新的返回棧)。那么這樣做有什么意義呢?想象以下場景,假設(shè)我們的程序中有一個(gè)activity是允許其他程序調(diào)用的,如果我們想實(shí)現(xiàn)其他程序和我們的程序可以共享這個(gè)activity的實(shí)例,應(yīng)該如何實(shí)現(xiàn)呢?使用前面三種啟動(dòng)模式肯定是做不到的,因?yàn)槊總€(gè)應(yīng)用程序都會(huì)有自己的返回棧,同一個(gè)activity在不同的返回棧中入棧時(shí)必然是創(chuàng)建了新的實(shí)例。而使用singleInstance模式就可以解決這個(gè)問題,在這種模式下會(huì)有一個(gè)單獨(dú)的返回棧來管理這個(gè)activity,不管是哪個(gè)應(yīng)用程序來訪問這個(gè)activity,都共用的同一個(gè)返回棧,也就解決了共享activity實(shí)例的問題。
順便看下api文檔的介紹片段:摘自/guide/topics/manifest/activity-element.html#lmodeandroid:launchModeAninstructiononhowtheactivityshouldbelaunched.Therearefourmodes
thatworkinconjunctionwithactivityflags(FLAG_ACTIVITY_*constants)inIntentobjectsto
determinewhatshouldhappenwhentheactivityiscalledupontohandlean
intent.Theyare:"standard""singleTop"
"singleTask""singleInstance"Thedefaultmodeis"standard".Asshowninthetablebelow,themodesfallintotwomaingroups,with
"standard"and"singleTop"activitiesononeside,and
"singleTask"and"singleInstance"activitiesonthe
other.Anactivitywiththe"standard"or"singleTop"
launchmodecanbeinstantiatedmultipletimes.Theinstancescanbelongtoany
taskandcanbelocatedanywhereintheactivitystack.Typically,they're
launchedintothetaskthatcalledstartActivity()(unlesstheIntentobjectcontainsaFLAG_ACTIVITY_NEW_TASKinstruction,inwhichcaseadifferenttaskischosen—seethetaskAffinityattribute).Incontrast,"singleTask"and"singleInstance"
activitiescanonlybeginatask.Theyarealwaysattherootoftheactivity
stack.Moreover,thedevicecanholdonlyoneinstanceoftheactivityatatime
—onlyonesuchtask.The"standard"and"singleTop"modesdifferfrom
eachotherinjustonerespect:Everytimethere'sanewintentfora
"standard"activity,anewinstanceoftheclassiscreatedto
respondtothatintent.Eachinstancehandlesasingleintent.Similarly,anew
instanceofa"singleTop"activitymayalsobecreatedtohandlea
newintent.However,ifthetargettaskalreadyhasanexistinginstanceofthe
activityatthetopofitsstack,thatinstancewillreceivethenewintent(in
anonNewIntent()call);anewinstanceisnotcreated.Inothercircumstances—forexample,if
anexistinginstanceofthe"singleTop"activityisinthetarget
task,butnotatthetopofthestack,orifit'satthetopofastack,butnot
inthetargettask—anewinstancewouldbecreatedandpushedonthestack.Similarly,ifyounavigateuptoan
activityonthecurrentstack,thebehaviorisdeterminedbytheparent
activity'slaunchmode.Iftheparentactivityhaslaunchmode
singleTop(ortheupintentcontainsFLAG_ACTIVITY_CLEAR_TOP),
theparentisbroughttothetopofthestack,anditsstateispreserved.The
navigationintentisreceivedbytheparentactivity'sonNewIntent()method.Iftheparentactivityhaslaunchmodestandard(andthe
upintentdoesnotcontainFLAG_ACTIVITY_CLEAR_TOP),
thecurrentactivityanditsparentarebothpoppedoffthestack,andanew
instanceoftheparentactivityiscreatedtoreceivethenavigationintent.The"singleTask"and"singleInstance"modesalso
differfromeachotherinonlyonerespect:A"singleTask"activity
allowsotheractivitiestobepartofitstask.It'salwaysattherootofits
task,butotheractivities(necessarily"standard"and
"singleTop"activities)canbelaunchedintothattask.A
"singleInstance"activity,ontheotherhand,permitsnoother
activitiestobepartofitstask.It'stheonlyactivityinthetask.Ifit
startsanotheractivity,thatactivityisassignedtoadifferenttask—asif
FLAG_ACTIVITY_NEW_TASKwasintheintent.Asshowninthetableabove,standardisthedefaultmodeandis
appropriateformosttypesofactivities.SingleTopisalsoa
commonandusefullaunchmodeformanytypesofactivities.Theothermodes—
singleTaskandsingleInstance—arenotappropriateformostapplications,sincethey
resultinaninteractionmodelthatislikelytobeunfamiliartousersandis
verydifferentfrommostotherapplications.Regardlessofthelaunchmodethatyouchoose,makesureto
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年拉薩貨運(yùn)從業(yè)資格證考試試題及答案解析
- 保障性合同范本
- 區(qū)域總經(jīng)理合同范本
- 醫(yī)療就業(yè)合同范本
- 包食堂合同范本
- 促銷活動(dòng)場地出租合同范本
- 農(nóng)村電商合同范本
- 利用合同范本
- 前廳接待勞務(wù)合同范本
- 5人合作合同范本
- 七年級下冊《平行線的判定》課件與練習(xí)
- 2025年中考英語時(shí)文閱讀 6篇有關(guān)電影哪吒2和 DeepSeek的英語閱讀(含答案)
- 修高速土方合同范例
- 2024年湖北省武漢市中考語文試卷
- 二零二五年度高品質(zhì)小區(qū)瀝青路面翻新施工與道路綠化合同2篇
- 2024年形勢與政策復(fù)習(xí)題庫含答案(綜合題)
- 2022年北京市初三一模語文試題匯編:基礎(chǔ)知識綜合
- 2025年廣東食品藥品職業(yè)學(xué)院高職單招高職單招英語2016-2024年參考題庫含答案解析
- 2 爆破工試題及答案
- 電路基礎(chǔ)知到智慧樹章節(jié)測試課后答案2024年秋江西職業(yè)技術(shù)大學(xué)
- DCMM數(shù)據(jù)管理師練習(xí)測試卷
評論
0/150
提交評論