Activity生命周期(二)
------siwuxie95
在项目 ActivityLifeCircle 的 MainActivity.java 中添加方法:
onCreate() onStart() onResume() onPause() onStop() onRestart() onDestroy()
具体代码:
package com.siwuxie095.activitylifecircle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //日志信息 这里直接输入sout,即可自动补全代码 System.out.println("onCreate"); } @Override protected void onStart() { super.onStart(); System.out.println("onStart"); } @Override protected void onResume() { super.onResume(); System.out.println("onResume"); } @Override protected void onPause() { super.onPause(); System.out.println("onPause"); } @Override protected void onStop() { super.onStop(); System.out.println("onStop"); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } @Override protected void onRestart() { super.onRestart(); System.out.println("onRestart"); } }
如果安装了Android Virtual Device的话,就可以直接在控制台观察到Android程序的生命周期变化
1、首先打开程序,执行 onCreate onStart onResume
2、点击home键,回到桌面,执行 onPause onStop (此Activity已不可见)
3、点击多任务键,找到ActivityLifeCircle程序,点击打开,执行 onRestart onStart onResume
4、点击返回键,回到桌面,执行 onPause onStop onDestroy
5、再次点击多任务键,找到该程序,点击打开,执行 onCreate onStart onResume(即一个全新的生命周期)
在两个Activity切换过程中,两个Activity的生命周期变化:
在ActivityLifeCircle中new一个新的Empty Activity:BAty,工程结构如下:
将MainActivity.java中的七个方法中的日志信息改为A,同时为BAty.java添加7个方法,
并输出日志信息为B,如下:
MainActivity.java:
package com.siwuxie095.activitylifecircle; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //日志信息 这里直接输入sout,即可自动补全代码 System.out.println("A onCreate"); //通过按钮启动B Activity findViewById(R.id.btnStartBAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,BAty.class)); } }); } @Override protected void onStart() { super.onStart(); System.out.println("A onStart"); } @Override protected void onResume() { super.onResume(); System.out.println("A onResume"); } @Override protected void onPause() { super.onPause(); System.out.println("A onPause"); } @Override protected void onStop() { super.onStop(); System.out.println("A onStop"); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("A onDestroy"); } @Override protected void onRestart() { super.onRestart(); System.out.println("A onRestart"); } }
BAty.java:
package com.siwuxie095.activitylifecircle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class BAty extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baty); System.out.println("B onCreate"); } @Override protected void onStart() { super.onStart(); System.out.println("B onStart"); } @Override protected void onResume() { super.onResume(); System.out.println("B onResume"); } @Override protected void onPause() { super.onPause(); System.out.println("B onPause"); } @Override protected void onStop() { super.onStop(); System.out.println("B onStop"); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("B onDestroy"); } @Override protected void onRestart() { super.onRestart(); System.out.println("B onRestart"); } }
为activity_main.xml添加一个 TextView 和 Button,
为activity_baty.xml添加一个 TextView
activity_main.xml:
<?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:id="@+id/activity_main" android:layout_width="wrap_content" android:layout_height="wrap_content" 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="com.siwuxie095.activitylifecircle.MainActivity" android:baselineAligned="false" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是A Activity" android:layout_weight="1" /> <Button android:text="启动B Activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_toEndOf="@+id/textView2" android:layout_marginTop="79dp" android:id="@+id/btnStartBAty" /> </LinearLayout>
activity_baty.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_baty" 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="com.siwuxie095.activitylifecircle.BAty"> <TextView android:text="这是B Activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:layout_alignParentTop="true" /> </RelativeLayout>
1、打开程序,执行 A onCreate A onStart A onResume
2、点击 启动B Activity,执行 A onPause B onCreate B onStart B onResume A onResume
(即在B呈现出来之前,A还是可以呈现的,所以A 只执行了onPause,在B完全呈现出来之后,A才执行onStop)
这种情况是B是不透明的,完全遮住了A,如果B是透明的或没有遮住A,A的生命周期如下:
在AndroidManifest.xml的BAty:<activity>…</activity>标签中添加dialog类型的theme:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.siwuxie095.activitylifecircle"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BAty" android:theme="@style/Base.Theme.AppCompat.Dialog" > </activity> </application> </manifest>
此时点击 启动B Activity,执行A onPause B onCreate B onStart B onResume
没有执行A onStop
回到A Activity(回到A Activity只需点击周围空白即可),执行B onPause
A onResume B onStop B onDestroy (B onPause之后,A立马就 onResume,
然后才是B的onStop和onDestroy)
【made by siwuxie095】
posted on 2016-12-25 22:54 siwuxie095 阅读(303) 评论(0) 编辑 收藏 举报