Activity的生命周期(四)——多个Activity相互交互的生命周期

来源:互联网 发布:新浪网络经纪人 编辑:程序博客网 时间:2024/05/01 13:50
1、创建Activity_A --> 跳转到Activity_B --> 返回Activity_A:
(1)创建Activity_A:
Activity_A onCreate --> Activity_A onStart --> Activity_A onResume
(2)跳转到Activity_B(点击跳转按钮:
Activity_A onPause --> Activity_B onCreate --> Activity_B onStart --> Activity_B onResume --> Activity_A onStop
(3)返回Activity_A(点击返回键:

Activity_B onPause --> Activity_A onRestart --> Activity_A onStart --> Activity_B onResume --> Activity_B onStop --> Activity_B onDestroy


2、多个Activity相互交互的设计思想:
当前Activity先onPause再启动下一个Activity——联想听歌时打进电话

跳转Activity后,上一个Activity先隐藏而不是销毁——避免返回时黑屏


Activity_A代码:

public class ManyActivity_A extends Activity implements OnClickListener{private String TAG = "ManyActivity_A";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_many_a);Log.e(TAG, "onCreate");Button btn_switchToB = (Button) findViewById(R.id.btn_switchToB);btn_switchToB.setOnClickListener(this);}@Overrideprotected void onStart() {super.onStart();Log.e(TAG, "onStart");}@Overrideprotected void onRestart() {super.onRestart();Log.e(TAG, "onRestart");}@Overrideprotected void onResume() {super.onResume();Log.e(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.e(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop();Log.e(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.e(TAG, "onDestroy");}@Overridepublic void onClick(View v) {startActivity(new Intent(ManyActivity_A.this, ManyActivity_B.class));}}

Activity_B代码:

public class ManyActivity_B extends Activity {private String TAG = "ManyActivity_B";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_many_b);Log.e(TAG, "onCreate");}@Overrideprotected void onStart() {super.onStart();Log.e(TAG, "onStart");}@Overrideprotected void onRestart() {super.onRestart();Log.e(TAG, "onRestart");}@Overrideprotected void onResume() {super.onResume();Log.e(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.e(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop();Log.e(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.e(TAG, "onDestroy");}}

activity_many_a.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="com.imooc.activitystudy.OneActivity" >    <TextView        android:id="@+id/tv_act"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Activity_A" />    <Button         android:layout_below="@id/tv_act"        android:id="@+id/btn_switchToB"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="跳转到Activity_B" /></RelativeLayout>

activity_many_b.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="com.imooc.activitystudy.OneActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Activity_B" /></RelativeLayout>


0 0