Android Activity生命周期举例说明(图文)
同学电话面试提及到Activity生命周期的详细说明,现整理如下:
转载请标明姓名及出处:大飞_Rflyee - http://blog.csdn.net/rflyee/article/details/9055547
必备知识背景:(详见官网:
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 - http://blog.csdn.net/rflyee/article/details/9055547
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
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- 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/textview1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="this is the first activity!" />
- <Button
- android:id="@+id/button1"
- android:layout_below="@id/textview1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Next Activity"/>
- </RelativeLayout>
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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/next_textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="this the second activity!"/>
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.myactivitylife"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="10"
- 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.myactivitylife.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=".SecindActivity">
- </activity>
- </application>
- </manifest>
情况二、
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)
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.myactivitylife"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="10"
- 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.myactivitylife.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=".SecindActivity"
- <strong>android:theme="@android:style/Theme.Dialog</strong>">
- </activity>
- </application>
- </manifest>
转载请标明作者及出处:大飞_Rflyee - http://blog.csdn.net/rflyee/article/details/9055547