Android基礎之Android的Activity周期

來源:互聯網
上載者:User

直接上代碼:

第一個類:TestActivity

package feb.hxy;import feb.hxy.R.string;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class TestActivity extends Activity {private Button button;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);System.out.println("TestActivity.onCreate");button = (Button) findViewById(R.id.anotherActivity);button.setText(string.anotherActivity);button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {Intent intent = new Intent();intent.setClass(TestActivity.this, AnothorAcftivity.class);startActivity(intent);TestActivity.this.finish();}});}public void onStart() {System.out.println("TestActivity.onStart");super.onStart();}public void onResume() {System.out.println("TestActivity.onResume");super.onResume();}public void onPause() {System.out.println("TestActivity.onPause");super.onPause();}public void onStop() {System.out.println("TestActivity.onStop");super.onStop();}public void onDestroy() {System.out.println("TestActivity.onDestory");super.onDestroy();}public void onRestart() {System.out.println("TestActivity.onRestart");super.onRestart();}}

第二個類:AnothorAcftivity

package feb.hxy;import feb.hxy.R.string;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class AnothorAcftivity extends Activity {private Button button;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);System.out.println("AnothorAcftivity.onCreate");button = (Button) findViewById(R.id.anotherActivity);button.setText(string.back);button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {Intent intent = new Intent();intent.setClass(AnothorAcftivity.this, TestActivity.class);startActivity(intent);//AnothorAcftivity.this.finish();}});}public void onStart() {System.out.println("AnothorAcftivity.onStart");super.onStart();}public void onResume() {System.out.println("AnothorAcftivity.onResume");super.onResume();}public void onPause() {System.out.println("AnothorAcftivity.onPause");super.onPause();}public void onStop() {System.out.println("AnothorAcftivity.onStop");super.onStop();}public void onDestroy() {System.out.println("AnothorAcftivity.onDestory");super.onDestroy();}public void onRestart() {System.out.println("AnothorAcftivity.onRestart");super.onRestart();}}

在模擬器中運行,查看DDMS,首先看到如下內容:

點擊程式中的按鈕,如下:

同樣也點擊第二個Activity中的按鈕組件,如下:

注意:並沒有調用到AnotherActivity的onDestroy()方法,因為AnotherActivity中注釋了:

//AnothorAcftivity.this.finish();

由此可見當調用finish()時會調用onDestroy()。此時銷毀Activity。

在點擊模擬器上的返回鍵,結果如下:

此時調用了AnotherActivity的onRestart()方法。

去掉AnotherActivity中的注釋,按照以上的執行步驟,運行結果如下:

完整代碼下載:TestAndroid(註:本程式使用的是2.2的模擬器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.