深入Activity生命周期(二)
下面以Demo来帮助理解Activity生命周期,
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="so many people." /> <EditText android:id="@+id/et" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="hello,world" /> </LinearLayout>
只显示一个TextView和一个EditView,
Activity代码如下:
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.EditText; import android.widget.TextView; public class AndroidOrientationtestActivity extends Activity { EditText et; TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initUI(); Log.i("my", "onCreate---"); } private void initUI() { et = (EditText)findViewById(R.id.et); //打印控件的地址,对比切屏前的地址是否为同一个 Log.i("my", "et="+et.toString()); tv = (TextView)findViewById(R.id.tv); //打印控件的地址,对比切屏前的地址是否为同一个 Log.i("my", "tv="+tv.toString()); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i("my", "onRestart---"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i("my", "onStart---"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i("my", "onResume---"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i("my", "onPause---"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i("my", "onStop---"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("my", "onDestroy---"); } }
当启动Activity界面的时候,Log如下:
07-30 09:54:26.793: INFO/my(833): et=android.widget.EditText@4051ab70 07-30 09:54:26.793: INFO/my(833): tv=android.widget.TextView@4051a428 07-30 09:54:26.813: INFO/my(833): onCreate--- 07-30 09:54:26.813: INFO/my(833): onStart--- 07-30 09:54:26.813: INFO/my(833): onResume---
可以看到首先启动的是onCreate(),然后是onStart(),然后是onResume(),看得到有了界面。
当按下home键的时候,log如下:
07-30 09:54:26.793: INFO/my(833): et=android.widget.EditText@4051ab70 07-30 09:54:26.793: INFO/my(833): tv=android.widget.TextView@4051a428 07-30 09:54:26.813: INFO/my(833): onCreate--- 07-30 09:54:26.813: INFO/my(833): onStart--- 07-30 09:54:26.813: INFO/my(833): onResume--- 07-30 09:56:31.183: INFO/my(833): onPause--- 07-30 09:56:32.018: INFO/my(833): onStop---
可以看到多了onPause(),以及onStop(),
再次按下home键,切回去Activity,Log如下:
07-30 09:54:26.793: INFO/my(833): et=android.widget.EditText@4051ab70 07-30 09:54:26.793: INFO/my(833): tv=android.widget.TextView@4051a428 07-30 09:54:26.813: INFO/my(833): onCreate--- 07-30 09:54:26.813: INFO/my(833): onStart--- 07-30 09:54:26.813: INFO/my(833): onResume--- 07-30 09:56:31.183: INFO/my(833): onPause--- 07-30 09:56:32.018: INFO/my(833): onStop--- 07-30 09:57:49.303: INFO/my(833): onRestart--- 07-30 09:57:49.303: INFO/my(833): onStart--- 07-30 09:57:49.303: INFO/my(833): onResume---
在这里执行了onRestart(),onStart(),onResume()方法。
这里因为系统决定回收Activity,这里无法模拟当Activity被停止,然后被回收以后的状态,但是生命周期与新启动一个activity是一样的。
如果这时候按下call键,则会弹出对话框,这时候会局部挡住当前的Activity,然后再按返回键,log如下:
07-30 09:54:26.793: INFO/my(833): et=android.widget.EditText@4051ab70 07-30 09:54:26.793: INFO/my(833): tv=android.widget.TextView@4051a428 07-30 09:54:26.813: INFO/my(833): onCreate--- 07-30 09:54:26.813: INFO/my(833): onStart--- 07-30 09:54:26.813: INFO/my(833): onResume--- 07-30 09:56:31.183: INFO/my(833): onPause--- 07-30 09:56:32.018: INFO/my(833): onStop--- 07-30 09:57:49.303: INFO/my(833): onRestart--- 07-30 09:57:49.303: INFO/my(833): onStart--- 07-30 09:57:49.303: INFO/my(833): onResume--- 07-30 10:01:20.093: INFO/my(833): onPause--- 07-30 10:01:32.574: INFO/my(833): onResume---
当对话框挡住activity时,调用了onPause,按下返回键时,重新onResume().