首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Activity札记(5)-Activity生命周期

2013-08-01 
Activity笔记(5)---Activity生命周期?创建一个测试项目ActivityLife在MainActivity.java中重写onCreate,on

Activity笔记(5)---Activity生命周期

?

创建一个测试项目ActivityLife在MainActivity.java中重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法

?

package com.example.activitylife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private static final String TAG = "MainActivity";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.e(TAG, "~~~onCreate~~~");    }    @Override    protected void onStart() {        super.onStart();        Log.e(TAG, "~~~onStart~~~");    }    @Override    protected void onRestart() {        super.onRestart();        Log.e(TAG, "~~~onReStart~~~");    }    @Override    protected void onResume() {        super.onResume();        Log.e(TAG, "~~~onResume~~~");    }    @Override    protected void onPause() {        super.onPause();        Log.e(TAG, "~~~onPause~~~");    }    @Override    protected void onStop() {        super.onStop();        Log.e(TAG, "~~~onStop~~~");    }    @Override    protected void onDestroy() {        super.onDestroy();        Log.e(TAG, "~~~onDestroy~~~");    }}

?

运行该应用程序下面是不同操作对应的LogCat输出打开应用程序时,对应下图过程1
Activity札记(5)-Activity生命周期
?打开应用程序后,按下BACK键时,对应下图过程2
Activity札记(5)-Activity生命周期
?打开应用程序后,按下HOME键时,对应下图过程3
Activity札记(5)-Activity生命周期
?在上一操作的基础上,再次打开应用程序时,对应下图过程4Activity札记(5)-Activity生命周期
操作流程图:Activity札记(5)-Activity生命周期
?========================================================================
参考文章提出的一个问题:EditText在状态1时输入一个字符串,如"Hello,Android!",再经过3,4状态后无法保持看到有评论说 ?有id的view,android会自动维护状态经测试,无论EditText无论有无id,字符串都可保持住测试如下:修改res/layout/activity_main.xml,在布局中添加一个EditText控件
<? xml version ="1.0" encoding= "utf-8" ?>< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"    xmlns:tools ="http://schemas.android.com/tools"    android:layout_width ="fill_parent"    android:layout_height ="fill_parent"    android:orientation ="vertical"    android:paddingBottom ="@dimen/activity_vertical_margin"    android:paddingLeft ="@dimen/activity_horizontal_margin"    android:paddingRight ="@dimen/activity_horizontal_margin"    android:paddingTop ="@dimen/activity_vertical_margin"    tools:context =".MainActivity" >    <TextView        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:text ="@string/hello_world" />    <EditText        android:id ="@+id/editText"        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:inputType ="text" /></ LinearLayout>
?再依次运行过程1-->3-->4在状态1的时候,在EditText控件中写入一个字符串,如Hello,Android!,在经过3,4过程后字符串依旧保持
Activity札记(5)-Activity生命周期
? ========================================================================测试状态5修改res/layout/main_activity.xml,添加一个Button
<? xml version ="1.0" encoding= "utf-8" ?>< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"    xmlns:tools ="http://schemas.android.com/tools"    android:layout_width ="fill_parent"    android:layout_height ="fill_parent"    android:orientation ="vertical"    android:paddingBottom ="@dimen/activity_vertical_margin"    android:paddingLeft ="@dimen/activity_horizontal_margin"    android:paddingRight ="@dimen/activity_horizontal_margin"    android:paddingTop ="@dimen/activity_vertical_margin"    tools:context =".MainActivity" >    <TextView        android:id = "@+id/mainTv"        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:text ="@string/hello_world" />    <EditText        android:id = "@+id/mainEt"        android:layout_width= "fill_parent"        android:layout_height ="wrap_content"        android:inputType ="text" />    <Button        android:id ="@+id/callDialog"        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:text ="@string/call_dialog" /></ LinearLayout>
?添加一个Activity,SecondActivity并重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法
package com.example.activitylife;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class SecondActivity extends Activity {    private static final String TAG = "SecondActivity";        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);                Log.e(TAG, "~~~onCreate~~~");    }    @Override    protected void onStart() {        super.onStart();        Log.e(TAG, "~~~onStart~~~");    }    @Override    protected void onRestart() {        super.onRestart();        Log.e(TAG, "~~~onReStart~~~");    }    @Override    protected void onResume() {        super.onResume();        Log.e(TAG, "~~~onResume~~~");    }    @Override    protected void onPause() {        super.onPause();        Log.e(TAG, "~~~onPause~~~");    }    @Override    protected void onStop() {        super.onStop();        Log.e(TAG, "~~~onStop~~~");    }    @Override    protected void onDestroy() {        super.onDestroy();        Log.e(TAG, "~~~onDestroy~~~");    }}
?修改MainActivity.java
package com.example.activitylife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private static final String TAG = "MainActivity";    private Button callDialogBut;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.e(TAG, "~~~onCreate~~~");        callDialogBut = (Button) findViewById(R.id.callDialog);        callDialogBut.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this, SecondActivity.class);                startActivity(intent);            }        });    }    @Override    protected void onStart() {        super.onStart();        Log.e(TAG, "~~~onStart~~~");    }    @Override    protected void onRestart() {        super.onRestart();        Log.e(TAG, "~~~onReStart~~~");    }    @Override    protected void onResume() {        super.onResume();        Log.e(TAG, "~~~onResume~~~");    }    @Override    protected void onPause() {        super.onPause();        Log.e(TAG, "~~~onPause~~~");    }    @Override    protected void onStop() {        super.onStop();        Log.e(TAG, "~~~onStop~~~");    }    @Override    protected void onDestroy() {        super.onDestroy();        Log.e(TAG, "~~~onDestroy~~~");    }}
?修改AndroidManifest.xml文件
<? xml version ="1.0" encoding= "utf-8" ?>< manifest xmlns:android ="http://schemas.android.com/apk/res/android"    package ="com.example.activitylife"    android:versionCode ="1"    android:versionName ="1.0" >    <uses-sdk        android:minSdkVersion ="8"        android:targetSdkVersion ="17" />    <application        android:allowBackup ="true"        android:icon ="@drawable/ic_launcher"        android:label ="@string/app_name"        android:theme ="@style/AppTheme" >        < activity            android:name ="com.example.activitylife.MainActivity"            android:label ="@string/app_name" >            < intent-filter>                < action android:name ="android.intent.action.MAIN" />                < category android:name ="android.intent.category.LAUNCHER" />            </ intent-filter>        </ activity>        < activity            android:name =".SecondActivity"            android:label ="@string/app_name"            android:theme ="@android:style/Theme.Dialog" >        </ activity>    </application ></ manifest>
?启动应用程序后,点击 调用对话框 按钮,相当于过程5中的onPause
Activity札记(5)-Activity生命周期
?在上一步操作的基础上,点击BACK键,相当于过程5中的onResume
Activity札记(5)-Activity生命周期
?程序运行截图:启动应用程序
Activity札记(5)-Activity生命周期?按下 调用对话框 按钮
Activity札记(5)-Activity生命周期
?再按下BACKUP键
Activity札记(5)-Activity生命周期
??

?

参考文章:

-----------------------

两分钟彻底让你明白Android Activity生命周期(图文)!

热点排行