同学电话面试提及到Activity生命周期的详细说明,现整理如下:
转载请标明姓名及出处:大飞_Rflyee -
必备知识背景:(详见官网:
1、Activity生命周期常见的七种方法:onCreate()、onStart()、onRestart()、onResume()、onPause()、onStop()、onDestroy()
2、调用顺序规则(官网)
下面以“Activity A转到B,再返回A”为例,说明各方法调用顺序:
情况一、B将A完全遮挡
调用顺序:
A:onCreate() - onStart() - onResume() - onPause()
B:onCreate() - onStart() - onResume()
A:onStop()
B:onPause()
A:onRestart() - onStart() - onResume()
B :onStop() - onDestroy()
实例:
启动应用,第一个activity
logCat视图如下:
启动第二个activity,将第一个完全覆盖
logCat视图如下:
按下返回键,重现回到第一个activity之后logCat视图如下:
附源代码(仅供参考),转载请标明作者及出处:大飞_Rflyee -
MainActivity.java
package com.example.myactivitylife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private static final String TAG = "--activityLife--"; private Button bt1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button)findViewById(R.id.button1); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SecindActivity.class); startActivity(intent); } }); Log.i(TAG, "onCreate_first"); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "onStart_first"); } @Override public void onRestart(){ super.onRestart(); Log.i(TAG, "onRestart_first"); } @Override public void onResume(){ super.onResume(); Log.i(TAG, "onResume_first"); } @Override public void onPause(){ super.onPause(); Log.i(TAG,"onPause_first"); } @Override public void onStop(){ super.onStop(); Log.i(TAG, "onStop_first"); } @Override public void onDestroy(){ super.onDestroy(); Log.i(TAG, "onDestroy_first"); } @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; }}
activity_main.xml
SecondActivity.java
package com.example.myactivitylife;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class SecindActivity extends Activity{ private final String tag = "--dialog--"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Log.i(tag, "onCreate_second"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(tag, "onDestroy_second"); } @Override protected void onPause() { super.onPause(); Log.i(tag, "onPause_second"); } @Override protected void onRestart() { super.onRestart(); Log.i(tag, "onRestart_second"); } @Override protected void onResume() { super.onResume(); Log.i(tag, "onResume_second"); } @Override protected void onStart() { super.onStart(); Log.i(tag, "onStart_second"); } @Override protected void onStop() { super.onStop(); Log.i(tag, "onStop_second"); } }
second.xml
AndroidManifest.xml
情况二、B不能将A完全遮挡
调用顺序:
A:onCreate() - onStart() - onResume() - onPause()
B:onCreate() - onStart() - onResume() - onPause()
A:onResume()
B :onStop() - onDestroy()
实例效果图:
启动A - 启动B -返回A整个过程的logCat视图:
附源代码(仅供参考),
只需将情况一中的AndroidManifest.xml稍作改动即可(将第二个activity的Theme声明为Theme.Dialog)
转载请标明作者及出处:大飞_Rflyee -