上代码MainAcitivity
package com.study.administrator.mystudysomething; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextView here; private final static String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { Log.w(TAG,"onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { here= (TextView) findViewById(R.id.here); here.setClickable(true); here.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.here: Intent myIntent = new Intent(this, SecondActivity.class); startActivity(myIntent); // startActivityForResult(myIntent,111); break; } } @Override protected void onStart() { Log.w(TAG,"onStart"); super.onStart(); } @Override protected void onResume() { Log.w(TAG,"onResume"); super.onResume(); } @Override protected void onPause() { Log.w(TAG,"onPause"); super.onPause(); } @Override protected void onStop() { Log.w(TAG,"onStop"); super.onStop(); } @Override protected void onDestroy() { Log.w(TAG,"onDestroy"); super.onDestroy(); } @Override protected void onRestart() { Log.w(TAG,"onRestart"); super.onRestart(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.w(TAG,"onActivityResult"); super.onActivityResult(requestCode, resultCode, data); } }
activity_main
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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.study.administrator.mystudysomething.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是页面A,------跳转到下个页面B" android:id="@+id/here" /> RelativeLayout>
SecondActivity
package com.study.administrator.mystudysomething; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import org.w3c.dom.Text; public class SecondActivity extends AppCompatActivity implements View.OnClickListener{ private final static String TAG="MainActivity"; private TextView there; @Override protected void onCreate(Bundle savedInstanceState) { Log.w(TAG,"BonCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); initView(); } private void initView() { there= (TextView) findViewById(R.id.there); there.setClickable(true); there.setOnClickListener(this); } @Override protected void onStart() { Log.w(TAG,"BonStart"); super.onStart(); } @Override protected void onResume() { Log.w(TAG,"BonResume"); super.onResume(); } @Override protected void onPause() { Log.w(TAG,"BonPause"); super.onPause(); } @Override protected void onStop() { Log.w(TAG,"BonStop"); super.onStop(); } @Override protected void onDestroy() { Log.w(TAG,"BonDestroy"); super.onDestroy(); } @Override protected void onRestart() { Log.w(TAG,"BonRestart"); super.onRestart(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.there: Intent myIntent = new Intent(this, MainActivity.class); startActivity(myIntent); // finish(); break; } } }
activity_second
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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.study.administrator.mystudysomething.SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是页面B,------跳转到下个页面A" android:id="@+id/there" /> RelativeLayout>
下面分三个步骤讨论:
1,当A Intent跳转到B 然后B Intent再跳转到A的时候:
(1)A Intent B
12-21 18:22:36.692 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onPause
12-21 18:22:36.742 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-21 18:22:36.762 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-21 18:22:36.762 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-21 18:22:36.842 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onStop
(2)B intent A
12-21 18:23:46.026 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-21 18:23:46.036 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onCreate
12-21 18:23:46.056 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onStart
12-21 18:23:46.066 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onResume
12-21 18:23:46.156 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonStop
这说明 每一次Intent 都建立一个新的Activity的生命周期
然后按回退键
12-21 18:25:10.886 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onPause
12-21 18:25:10.896 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonRestart
12-21 18:25:10.896 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-21 18:25:10.896 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-21 18:25:10.966 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onStop
12-21 18:25:10.966 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onDestroy
1 18:25:21.528 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-21 18:25:21.538 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onRestart
12-21 18:25:21.538 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onStart
12-21 18:25:21.538 29359-29359/com.study.administrator.mystudysomething W/MainActivity: onResume
12-21 18:25:21.608 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonStop
12-21 18:25:21.608 29359-29359/com.study.administrator.mystudysomething W/MainActivity: BonDestroy
这说明 按回退键 会把当前的Activity给Destroy掉 并且回退的那个Activity调用onRestart方法
2,如果A Intent跳到B B执行finish方法 就是这样子的日志
12-22 10:14:04.112 11952-11952/com.study.administrator.mystudysomething W/MainActivity: onPause
12-22 10:14:04.172 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-22 10:14:04.182 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 10:14:04.182 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 10:14:04.282 11952-11952/com.study.administrator.mystudysomething W/MainActivity: onStop
12-22 10:14:49.500 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 10:14:49.510 11952-11952/com.study.administrator.mystudysomething W/MainActivity: onRestart
12-22 10:14:49.510 11952-11952/com.study.administrator.mystudysomething W/MainActivity: onStart
12-22 10:14:49.510 11952-11952/com.study.administrator.mystudysomething W/MainActivity: onResume
12-22 10:14:49.591 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonStop
12-22 10:14:49.591 11952-11952/com.study.administrator.mystudysomething W/MainActivity: BonDestroy
比Intent跳转多加了一个onDestroy
3,使用A startActivityForResult 跳转到B 再由B finish掉跳转到A
A startActivityForResult 跳转到B
12-22 10:18:53.030 13205-13205/com.study.administrator.mystudysomething W/MainActivity: onPause
12-22 10:18:53.080 13205-13205/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-22 10:18:53.100 13205-13205/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 10:18:53.100 13205-13205/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 10:18:53.190 13205-13205/com.study.administrator.mystudysomething W/MainActivity: onStop
Bfinish掉
12-22 10:44:55.958 31354-31354/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 10:44:55.968 31354-31354/com.study.administrator.mystudysomething W/MainActivity: onActivityResult
12-22 10:44:55.968 31354-31354/com.study.administrator.mystudysomething W/MainActivity: onRestart
12-22 10:44:55.968 31354-31354/com.study.administrator.mystudysomething W/MainActivity: onStart
12-22 10:44:55.968 31354-31354/com.study.administrator.mystudysomething W/MainActivity: onResume
12-22 10:44:56.058 31354-31354/com.study.administrator.mystudysomething W/MainActivity: BonStop
12-22 10:44:56.058 31354-31354/com.study.administrator.mystudysomething W/MainActivity: BonDestroy
这里表明 startActivityForResult 跳转到B 走的生命周期和之前一样 如果finish掉B的话 返回A的时候 先调用onActivityResult 然后再调用Activity的OnRestart生命后期
这只研究了一半 如果对生命周期更深的研究就要研究一下四种启动方式:
standard,
singleTop,
singleTask,
singleInstance
standard是默认的启动方法,可以不用写配置。在这个模式下,都会默认创建一个新的实例。因此,在这种模式下,可以有多个相同的实例,也允许多个相同Activity叠加。
也是上面生命周期所演示的
singleTop
可以有多个实例,但是不允许多个相同Activity叠加。即,如果Activity在栈顶的时候,启动相同的Activity,不会创建新的实例,而会调用其onNewIntent方法。
友情奉献SDK中API的onNewIntent方法:
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP
flag when calling startActivity(Intent)
. In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
An activity will always be paused before receiving a new intent, so you can count on onResume()
being called after this method.
Note that getIntent()
still returns the original Intent. You can use setIntent(Intent)
to update it to this new Intent.
intent | The new intent that was started for the activity. |
---|
getIntent()
setIntent(Intent)
onResume()
An activity will always be paused before receiving a new intent, so you can count on onResume()
being called after this method.
这句话的大意是:一个Activity通常被暂停 在接收到一个新的intent之前,所以 你可以指望onResume()被调用 在这个方法之后
所以 就是onPause 之后调用onResume
下面设置B为singleTop
A跳到B:
12-22 11:11:54.455 23350-23350/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:11:54.515 23350-23350/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-22 11:11:54.515 23350-23350/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 11:11:54.515 23350-23350/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 11:11:54.595 23350-23350/com.study.administrator.mystudysomething W/MainActivity: AonStop
B跳到B:
12-22 11:12:19.852 23350-23350/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 11:12:19.852 23350-23350/com.study.administrator.mystudysomething W/MainActivity: BonResume
singleTask
只有一个实例。在同一个应用程序中启动他的时候,若Activity不存在,则会在当前task创建一个新的实例,若存在,则会把task中在其之上的其它Activity destory掉并调用它的onNewIntent方法。
下面设置B为singleTask
A跳转到B:
12-22 11:35:50.948 12517-12517/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:35:50.998 12517-12517/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-22 11:35:50.998 12517-12517/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 11:35:51.008 12517-12517/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 11:35:51.088 12517-12517/com.study.administrator.mystudysomething W/MainActivity: AonStop
B跳转到A
12-22 11:37:31.065 14155-14155/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 11:37:31.075 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonCreate
12-22 11:37:31.075 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonStart
12-22 11:37:31.075 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonResume
12-22 11:37:31.165 14155-14155/com.study.administrator.mystudysomething W/MainActivity: BonStop
A跳转到B
12-22 11:38:01.057 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:38:01.067 14155-14155/com.study.administrator.mystudysomething W/MainActivity: BonRestart
12-22 11:38:01.067 14155-14155/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 11:38:01.067 14155-14155/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 11:38:01.127 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonStop
12-22 11:38:01.127 14155-14155/com.study.administrator.mystudysomething W/MainActivity: AonDestroy
这里 和上边不是一样 再跳转到B的时候 B调用onRestart方法 也难怪在Activity调用到onStop的生命周期的时候 onRestart才能拯救回来 如果调用到OnPause的时候 onResume方法直接就拯救回来了
singleInstance
只有一个实例,并且这个实例独立运行在一个task中,这个task只有这个实例,不允许有别的Activity存在。
B设置为singleInstance
A跳转到B
12-22 11:49:37.640 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:49:37.650 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonCreate
12-22 11:49:37.650 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 11:49:37.660 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 11:49:37.720 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStop
B跳转到A
12-22 11:49:57.902 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 11:49:57.922 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonCreate
12-22 11:49:57.922 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStart
12-22 11:49:57.922 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonResume
12-22 11:49:58.002 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonStop
A跳转到B
12-22 11:50:29.976 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:50:29.986 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonRestart
12-22 11:50:29.986 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonStart
12-22 11:50:29.986 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonResume
12-22 11:50:30.046 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStop
按回退键1次
12-22 11:51:27.397 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonPause
12-22 11:51:27.407 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonRestart
12-22 11:51:27.407 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStart
12-22 11:51:27.407 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonResume
12-22 11:51:27.477 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonStop
12-22 11:51:27.477 21227-21227/com.study.administrator.mystudysomething W/MainActivity: BonDestroy
按回退键2次
12-22 11:52:04.697 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:52:04.707 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonRestart
12-22 11:52:04.707 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStart
12-22 11:52:04.707 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonResume
12-22 11:52:04.807 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStop
12-22 11:52:04.817 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonDestroy
按回退键3次
12-22 11:52:29.073 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonPause
12-22 11:52:29.243 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonStop
12-22 11:52:29.243 21227-21227/com.study.administrator.mystudysomething W/MainActivity: AonDestroy
我们得知:新的回退栈只有Activity B 一个Activity
回退栈回退的时候 一个回退栈退完了 再回退另外一个回退栈