Android生命週期裡你或許不知道的事

來源:互聯網
上載者:User

標籤:android   des   blog   http   java   color   

Android生命週期估計連初學者都再熟悉不過的東西了,但這裡我拋出幾個問題,或許大家以前沒有想過或者可能認識的有些錯誤。

一、當A啟動B時,A和B生命週期方法執行的先後順序是怎樣的?當按返回鍵返回時,又是怎樣的?(讀者可以先想想,可能會跟你的答案不一致)

A--->B時,列印結果如下:


按返回鍵B--->A,列印結果如下:


結論:先執行當前顯示Activity的onPause方法,接著執行完將要顯示Activity的生命週期方法,最後再執行當前顯示Activity的其它生命週期方法

二、生命週期onPause 彈出的新表單是指Activity的還是任意的,比如Dialog

上代碼:

import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class AActivity extends Activity {private static final String TMG = "ActivityTask";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i(TMG, "AActivity onCreate");setContentView(R.layout.layout_task);Button _buttonStartB = (Button) findViewById(R.id.buttonStartB);_buttonStartB.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog.Builder builder = new AlertDialog.Builder(AActivity.this);builder.setTitle("Pick a color");AlertDialog alert = builder.create();alert.show();}});}@Overrideprotected void onResume() {super.onResume();Log.i(TMG, "AActivity onResume");}@Overrideprotected void onPause() {super.onPause();Log.i(TMG, "AActivity onPause");}@Overrideprotected void onRestart() {super.onRestart();Log.i(TMG, "AActivity onRestart");}@Overrideprotected void onStart() {super.onStart();Log.i(TMG, "AActivity onStart");}@Overrideprotected void onStop() {super.onStop();Log.i(TMG, "AActivity onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.i(TMG, "AActivity onDestroy");}}

執行完代碼之後,點擊彈出對話方塊按鈕我們發現AActivity的生命週期方法沒有列印

結論:onPause彈出的新表單必須是Activity才行,如果是Dialog,生命週期方法是不執行的。



聯繫我們

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