編輯:關於Android編程
大家在碼代碼的時候,每個activity肯定是會調用onCreate方法的,但是onCreate方法中的savedInstanceState這個參數卻很少用到,經常就這麼被忽略了,從其命名來看,應該是用來保存activity狀態的,而這些狀態數據是從何而來呢?就是自己這回要學習的onSaveInstanceState方法,沒有什麼資料比官方文檔更准確詳細的了, onSavedInstanceState方法的翻譯,記錄學習一下該方法的使用。
protected void onSaveInstanceState (Bundle outState)
Added in API level 1
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
在一個activity被殺掉之前調用來保存每一個實例的狀態,這樣一來該狀態便可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) 中恢復。
------------------------------------------------------------------------------
This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).
該方法在一個activity即將被殺掉之前調用,以便於在將來某一時刻返回時可以恢復到其原來的狀態。比如,如果activityB啟動後位於activityA的前面,並且在某一時刻activityA由於系統回收資源要被殺掉,A就可以通過該方法將有機會保存其用戶界面的狀態,這樣,以後當用戶返回到A的時候,可以通過調用onCreate(Bundle)或者onRestoreInstanceState(Bundle)方法來恢復界面的狀態。
------------------------------------------------------------------------------
Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.
不要將這個方法和activity生命周期回調方法例如onPause()和onStop()搞混了,onPause()在activity被放置後台或者自行銷毀時被調用,onStop()是activity被銷毀時調用。有這樣一個說明什麼時候onPause和onStop被調用而不是onSaveInstanceState被調用的例子就是當用戶從activityB返回到activityA的時候:沒有必要調用B的onSaveInstanceState(Bundle)因為此時的B的實例永遠不會被恢復,所以系統會避免調用它。另一個調用onPause()而不是調用onSaveInstanceState()的例子是當activityB啟動並處於activityA的前端:如果在B的生命周期裡A的界面用戶狀態未被破話,系統就不會調用activityA 的onSaveInstanceState(Bundle)方法。
------------------------------------------------------------------------------
If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
默認的實現是通過調用UI層面上每個擁有ID的view的onSaveInstanceState()的方式來保存大部分UI實例的狀態,並且保存當前獲得焦點的view的id(所有的保存狀態都會在默認的onRestoreInstanceState(Bundle)實現中恢復)。如果你重寫這個方法來保存額外的沒有被每個單獨的view保存的信息,你可能想要通過在默認的實現中調用,或者自己來保存每個view的所有的狀態。
------------------------------------------------------------------------------
If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().
如果被調用,該方法會在onStop()之前發生。但並不保證是否在onPause()之前或之後發生。
Android圖表庫MPAndroidChart(十二)——正負堆疊條形圖。接上篇,今天要說的,和上篇的類似,只是方向是有相反的兩面,我們先看下效
有過一些面試經驗的人基本都深有體會,每次面試一般都會問到Fragment的知識,所以,今天我就單獨把Fragment拿出來與大家分享一下. 會涉及到Fragment如
之前網上看了下自定義消息欄,通知欄,了解到了Notification這個控件,發現UC浏覽器等都是這種類型,今天寫個demo實現下,如圖:其中每個按鈕都有不同的功能,代碼
本文實例為大家分享了Android仿微信發表說說、心情功能,供大家參考,具體內容如下既能實現拍照,選圖庫,多圖案上傳的案例,目前好多App都有類似微信朋友圈的功能,能過發