編輯:關於Android編程
上圖是Android Activity的生命周期圖,其中Resumed、Paused、StZ喎?/kf/ware/vc/" target="_blank" class="keylink">vcHBlZNe0zKzKx76yzKy1xKOs1eLI/bj217TMrM/CtcRBY3Rpdml0ebTm1NrKsbzkvc+zpKGjPC9wPgo8cD6jqDGjqVJlc3VtZWSjutTatMvXtMysyrGjrNPDu6e/ydLU0+tBY3Rpdml0eb340NC9u7ulo6xBY3Rpdml0edTa1+7HsLbLPC9wPgo8cD6jqDKjqVBhdXNlZKO61Nq0y9e0zKzKsaOsQWN0aXZpdHmxu8HtzeLSu7j2QWN0aXZpdHnV2rjHo6y0y0FjdGl2aXR5sru/yb3TytzTw7unyuTI69DFz6Kho8HtzeLSu7j2QWN0aXZpdHnAtLW91+7HsMPmo6yw6824w/e1xKOstauyorK7u+G4srjH1fu49sbBxLuhozwvcD4KPHA+o6gzo6lTdG9wcGVko7rU2rTL17TMrMqxo6xBY3Rpdml0ec3qyKuxu9L+stijrLK7v8m8+6GjsaPB9LWxx7DQxc+io6xBY3Rpdml0ebK71rTQ0MjOus60+sLroaM8L3A+CjxwPqOoNKOpQ3JlYXRlZNPrU3RhcnRlZKO6z7XNs7X308NvbkNyZWF0ZSgpuvPRuMvZtffTw29uU3RhcnQoKaOsyLu689G4y9nWtNDQb25SZXN1bWUoKaGjPC9wPgo8cD7S1MnPvs3Kx0FuZHJvaWS1xEFjdGl2aXR51fu49sn6w/zW3MbaoaM8L3A+CjxoMj4yoaLW90FjdGl2aXR5PC9oMj4KPHA+08O7p7/J0tTWuLaos8zQ8sb0tq+1xNb3vefD5qOstMvKsbG7yfnD986qobBsYXVuY2hlcrvybWFpbqGxQWN0aXZpdHm1xG9uQ3JlYXRlKCm3vbeosbu199PDo6yzyc6qs8zQ8rXEyOu/2rqvyv2ho7jDyOu/2kFjdGl2aXR5v8nS1NTaQW5kcm9pZE1hbmlmZXN0LnhtbNbQtqjS5db3QWN0aXZpdHmho7TLyrGjrNb3QWN0aXZpdHmx2NDryrnTw9LUz8Kx6sepyfnD96O6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">
系統首先調用新Activity的onCreate()方法,所以,我們必須實現onCreate()方法。如:聲明UI元素、定義成員變量、配置UI等。但是事情不宜太多,避免啟動程序太久而看不到界面。
TextView mTextView; // Member variable for text view in the layout @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the user interface layout for this Activity // The layout file is defined in the project res/layout/main_activity.xml file setContentView(R.layout.main_activity); // Initialize member TextView so we can manipulate it later mTextView = (TextView) findViewById(R.id.text_message); // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // For the main activity, make sure the app icon in the action bar // does not behave as a button ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(false); } }onCreate()執行完即調用onStart()和onResume()方法,Activity不會在Created或者Started狀態停留。
Activity的最後一個回調是onDestroy(),系統會執行這個方法做為你的Activity要從系統中完全刪除的信號。大多數APP不需實現此方法,因為局部類的references會隨著Activity的銷毀而銷毀。並且Activity應該在onPause()和onStop()方法中執行清楚Activity資源的操作。如果Activity在onCreate()時創建的後台線程,或者是其他有可能導致內存洩露的資源,你應該在onDestroy()時殺死它們。
@Override public void onDestroy() { super.onDestroy(); // Always call the superclass // Stop method tracing that the activity started during onCreate() android.os.Debug.stopMethodTracing(); }系統通常是在執行了onPause()與onStop()後在調用onDestroy(),除非在onCreate()中調用了finish()。例如,如果你的Activity只是做了一個臨時的邏輯跳轉功能,它使用用來決定跳轉到下一個Activity,這樣,你需要在onCreate()中調用finish()方法。系統就會直接調用onDestroy方法,其他生命周期就不會被執行。
@Override public void onPause() { super.onPause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (mCamera != null) { mCamera.release() mCamera = null; } }通常,不應該使用onPause()來保存用戶改變的數據到永久存儲上,當你確認用戶期待那些改變能夠自動保存的時候,才可以把那些數據存儲到永久存儲。然而,應該避免在onPause()時執行CPU-intensive的工作,例如寫數據到DB,因為他會導致切換Activity變得緩慢。這些工作應該放到onStop()中去坐。 如果,Activity實際上要被Stop,那麼應減少在onPause中的工作量,提高流暢性。 恢復Activity 用戶從Pause狀態恢復時,調用onResume()方法。此時Activity處於最前台,包括第一次創建時,此時,應該在onResume中初始化那些你在onPause方法裡釋放掉的組件,並執行那些Activity每次進入Resumed state都需要的初始化動作。
@Override public void onResume() { super.onResume(); // Always call the superclass method first // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera init } }
static final String STATE_SCORE = "playerScore"; static final String STATE_LEVEL = "playerLevel"; ... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ... }我們可以選擇實現onRestoreInstanceState(),而不是在onCreate方法裡恢復數據。onRestoreInstanceState()方法會在onStart()方法之後執行,系統僅僅會在存在需要恢復的狀態信息時才會調用onRestoreInstanceState(),因此不需檢查Bundle是否為NULL。
public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); }
首先看下效果圖布局文件:<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
基礎知識度量聲音強度,大家最熟悉的單位就是分貝(decibel,縮寫為dB)。這是一個無綱量的相對單位,計算公式如下:分子是測量值的聲壓,分母是參考值的聲壓(20微帕,人
第一篇博客裡面有介紹一篇關於自動換行實現諸多自定義控件跟各種效果的博文,但是礙於當初技術能力有限,寫的jar包裡的代碼亂七八糟,在最近忙完了手頭的工作,不經意間翻看了之前
Android 實現會旋轉的餅狀統計圖實例代碼最近在做一個項目,由於有需要統計的需要,於是就做成了下面餅狀統計圖。 下圖是效果圖: 大致思路是: 關於的介紹這裡不做詳細介