Android百議程序: Activity的生命期

來源:互聯網
上載者:User

標籤:des   android   blog   http   io   ar   os   使用   sp   

到了Android 4 的activity的生命期內的函數有7個,變得十分複雜,完善。故此要掌握好Activity的各個函數,知道應該在那個函數寫什麼邏輯代碼。

本篇部落格寫個小程式測試一下activity的聲明周期如何運作。


首先總結一下:

1 onCreate():activyt第一次建立的時候調用,還可能在改activity在onStop或者onPauce狀態下被系統自動釋放,那麼當使用者需要繼續回到改activity的時候,也會調用這個onCreate()函數

2 onStart():當改activity變為對使用者可視,那麼就調用這個函數,這個函數會比onCreate()調用更加頻發,調用了onCreate()函數緊跟著必然調用onStart(),還有在onStop狀態下,重新回到改activity,就會調用onCreate()函數,這個時候不用調用onCreate()函數。

3 onResume():當activity開始和使用者互動的時候就調用,一般是緊跟著onStart()調用,故此本函數又比onStart()函數調用的更加頻發,而且在onPauce狀態下,這個時候如果回到本activity介面的時候,就直接調用onResume()就可以了,不用調用onCreate(),當然就不用調用onCreate()函數了。

以上三個函數是一個activity在程式一開始必然調用的三個函數。

4 onPause():當前activity暫停,一般是被另外一個activity啟動,擋住了。

5 onStop():當前activity看不見了,一般會緊接著onPause()之後調用

6 onDestroy(): 在activity銷毀之前調用,記憶體緊缺會可能由系統銷毀,或者由使用者手動銷毀。

7 onRestart(): 在activity到了onStop狀態,但是沒有被銷毀,那麼恢複的時候就會調用這個函數,然後調用onStart(),然後調用onResume(),繼續運行。


下面是個引用Google上的十分清晰的圖:

出處:http://developer.android.com/reference/android/app/Activity.html



建立一個項目之後,使用如下代碼:

package su.lifeactivity.lifecycleofactivity;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private String TAG = "LifeCycle";private Button toastButton;//Call when the activity if first created.    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.d(TAG, "In the onCreate event");                toastButton = (Button) findViewById(R.id.toast_button);        toastButton.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View arg0) {new AlertDialog.Builder(MainActivity.this).setTitle(R.string.dialog).setItems(R.array.test,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialoginterface, int i) {}}).show();}                });    }    @Override    public void onStart() {    super.onStart();    Log.d(TAG, "In the onStart event");    }        @Override    public void onRestart(){    super.onRestart();    Log.d(TAG, "In the onRestart event");    }        @Override    public void onResume(){    super.onResume();    Log.d(TAG, "In the onResume event");    }        @Override    public void onPause(){    super.onPause();    Log.d(TAG, "In the onPause event");    }        @Override    public void onStop(){    super.onStop();    Log.d(TAG,  "In the onStop event");    }        @Override    public void onDestroy(){    super.onDestroy();    Log.d(TAG, "In the onDestroy event");    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

使用logcat輸出測試資訊,開啟logcat就能觀察輸出資訊。

具體測試是開啟關閉程式,在程式開啟的時候繼續開啟其他程式,如按打電話按鍵。就會看到Log.d(...)函數輸出各種提示資訊到logcat。

其中我增加一個按鍵,點擊就會彈出一個Alert dialog的,看會不會調用這些函數,實際測試結果是不會的。

或者使用Toast測試,結果是一樣的。


一些函數複寫的應用總結,引用Beginning android 4 application development-好像是一本中國人寫的外國出版的英文書,因為我看到作者的名字像中國人的姓名。

onCreate(): 建立和執行個體化需要在程式中用到的對象

onResume(): 啟動在activity在前景(為當前互動activity)的時候需要啟動並執行代碼或需要服務。

onPause(): 當前螢幕不可見這個activity的時候,需要暫停服務或代碼,這樣可以更加省記憶體,也就可以提高該activity不被系統關閉的可能。

onDestroy(): 釋放資源和儲存資源


Android百議程序: Activity的生命期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.