偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android -- Activity的銷(xiāo)毀和重建

移動(dòng)開(kāi)發(fā) Android
本文將介紹Activity的銷(xiāo)毀和重建。銷(xiāo)毀分兩種:第一種是正常的銷(xiāo)毀,比如用戶(hù)按下Back按鈕或者是activity自己調(diào)用了finish()方法;另一種是由于activity處于stopped狀態(tài),并且它長(zhǎng)期未被使用,或者前臺(tái)的activity需要更多的資源,這些情況下系統(tǒng)就會(huì)關(guān)閉后臺(tái)的進(jìn)程,以恢復(fù)一些內(nèi)存。

兩種銷(xiāo)毀

***種是正常的銷(xiāo)毀,比如用戶(hù)按下Back按鈕或者是activity自己調(diào)用了finish()方法;

另一種是由于activity處于stopped狀態(tài),并且它長(zhǎng)期未被使用,或者前臺(tái)的activity需要更多的資源,這些情況下系統(tǒng)就會(huì)關(guān)閉后臺(tái)的進(jìn)程,以恢復(fù)一些內(nèi)存。

需要注意的是這其中有一種情況就是屏幕旋轉(zhuǎn)的問(wèn)題,當(dāng)用戶(hù)旋轉(zhuǎn)手機(jī)屏幕,每一次都會(huì)導(dǎo)致activity的銷(xiāo)毀和重新建立。

在第二種情況下,盡管實(shí)際的activity實(shí)例已經(jīng)被銷(xiāo)毀,但是系統(tǒng)仍然記得它的存在,當(dāng)用戶(hù)返回到它的時(shí)候,系統(tǒng)會(huì)創(chuàng)建出一個(gè)新的實(shí)例來(lái)代替它,這里需要利用舊實(shí)例被銷(xiāo)毀時(shí)候存下來(lái)的數(shù)據(jù)。這些數(shù)據(jù)被稱(chēng)為“instance state”,是一個(gè)存在Bundle對(duì)象中的鍵值對(duì)集合。

缺省狀態(tài)下,系統(tǒng)會(huì)把每一個(gè)View對(duì)象保存起來(lái)(比如EditText對(duì)象中的文本,ListView中的滾動(dòng)條位置等),即如果activity實(shí)例被銷(xiāo)毀和重建,那么不需要你編碼,layout狀態(tài)會(huì)恢復(fù)到前次狀態(tài)。

但是如果你的activity需要恢復(fù)更多的信息,比如成員變量信息,則需要自己動(dòng)手寫(xiě)了。

onSaveInstanceState()

如果要存儲(chǔ)額外的數(shù)據(jù),必須覆寫(xiě)回調(diào)函數(shù)onSaveInstanceState().

系統(tǒng)會(huì)在用戶(hù)離開(kāi)activity的時(shí)候調(diào)用這個(gè)函數(shù),并且傳遞給它一個(gè)Bundle object,如果系統(tǒng)稍后需要重建這個(gè)activity實(shí)例,它會(huì)傳遞同一個(gè)Bundle object到onRestoreInstanceState() 和 onCreate() 方法中去。

1

當(dāng)系統(tǒng)停止activity時(shí),它會(huì)調(diào)用onSaveInstanceState()(過(guò)程1),如果activity被銷(xiāo)毀了,但是需要?jiǎng)?chuàng)建同樣的實(shí)例,系統(tǒng)會(huì)把過(guò)程1中的狀態(tài)數(shù)據(jù)傳給onCreate()和onRestoreInstanceState()(圖中標(biāo)出的2和3)。

存儲(chǔ)Activity狀

當(dāng)系統(tǒng)停止activity時(shí),系統(tǒng)會(huì)調(diào)用onSaveInstanceState(),狀態(tài)信息會(huì)以鍵值對(duì)的形式存儲(chǔ)下來(lái)。

默認(rèn)的實(shí)現(xiàn)中存儲(chǔ)了activity的view系列的狀態(tài),比如文本和滾動(dòng)條位置等。

要存儲(chǔ)額外的信息,必須自己實(shí)現(xiàn)onSaveInstanceState(),并且給Bundle object加上鍵值對(duì)。

  1. static final String STATE_SCORE = "playerScore"
  2. static final String STATE_LEVEL = "playerLevel"
  3. ... 
  4.  
  5. @Override 
  6. public void onSaveInstanceState(Bundle savedInstanceState) { 
  7.     // Save the user's current game state 
  8.     savedInstanceState.putInt(STATE_SCORE, mCurrentScore); 
  9.     savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); 
  10.      
  11.     // Always call the superclass so it can save the view hierarchy state 
  12.     super.onSaveInstanceState(savedInstanceState); 

要記得調(diào)用基類(lèi)的實(shí)現(xiàn),以實(shí)現(xiàn)默認(rèn)的實(shí)現(xiàn)。

恢復(fù)Activity狀態(tài)

當(dāng)activity重建時(shí),需要根據(jù)Bundle中的狀態(tài)信息數(shù)據(jù)恢復(fù)activity。onCreate() 和onRestoreInstanceState()回調(diào)函數(shù)都會(huì)接收到這個(gè)Bundle。

因?yàn)槊看蝿?chuàng)建新的activity實(shí)例的或重建一個(gè)實(shí)例的時(shí)候都會(huì)調(diào)用onCreate()方法,所以必須先檢查是否Bundle是null,如果是null,則表明是要?jiǎng)?chuàng)建一個(gè)全新的對(duì)象,而不是重建一個(gè)上次被銷(xiāo)毀的對(duì)象。

比如onCreate()方法可以這么寫(xiě):

  1. @Override 
  2. protected void onCreate(Bundle savedInstanceState) { 
  3.     super.onCreate(savedInstanceState); // Always call the superclass first 
  4.     
  5.     // Check whether we're recreating a previously destroyed instance 
  6.     if (savedInstanceState != null) { 
  7.         // Restore value of members from saved state 
  8.         mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
  9.         mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 
  10.     } else { 
  11.         // Probably initialize members with default values for a new instance 
  12.     } 
  13.     ... 

除了在onCreate()中恢復(fù)狀態(tài)外,也可以選擇在onRestoreInstanceState()中實(shí)現(xiàn),這個(gè)函數(shù)在onStart()之后調(diào)用。

只有在有數(shù)據(jù)要恢復(fù)的時(shí)候系統(tǒng)會(huì)調(diào)用onRestoreInstanceState(),所以不必檢查Bundle是否為null。

  1. public void onRestoreInstanceState(Bundle savedInstanceState) { 
  2.     // Always call the superclass so it can restore the view hierarchy 
  3.     super.onRestoreInstanceState(savedInstanceState); 
  4.     
  5.     // Restore state members from saved instance 
  6.     mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
  7.     mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 

此處也要注意,不要忘記調(diào)用基類(lèi)實(shí)現(xiàn)。

原文鏈接:http://www.w2bc.com/Article/4848

責(zé)任編輯:閆佳明 來(lái)源: w2bc
相關(guān)推薦

2014-08-08 10:36:12

ActivityAndroid

2011-05-30 14:00:35

Android Activity Intent

2015-10-20 15:54:16

android源碼滑動(dòng)關(guān)閉

2012-02-17 17:07:30

Android安全Activity劫持

2013-01-08 13:33:07

Android開(kāi)發(fā)Activity入門(mén)指南

2018-09-07 10:14:58

2011-05-19 17:49:08

ActivityAndroid開(kāi)發(fā)

2011-06-16 09:31:21

ActivityAndroid

2011-05-26 15:22:57

Android Activity

2010-09-01 16:44:26

SQL刪除主鍵

2011-06-02 11:13:10

Android Activity

2011-04-12 17:16:52

Activity跳轉(zhuǎn)開(kāi)發(fā)實(shí)例Android學(xué)習(xí)筆記

2010-02-06 10:14:36

Android Act

2011-06-01 17:35:35

Android Activity

2021-08-11 17:15:17

AndroidActivity場(chǎng)景

2010-11-16 09:18:39

oracle重建索引

2014-05-27 14:33:37

AndroidActivitysingleTask

2014-05-27 14:59:24

AndroidActivitysingleTask

2014-05-27 15:04:15

AndroidActivitysingleTask

2014-05-27 15:09:13

AndroidActivitysingleTask
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)