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

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";
	
	@Override
	protected 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);
	}
	
	@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");
	}

	@Override
	public 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";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_many_b);
		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");
	}
}

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>


你可能感兴趣的:(Activity的生命周期(四)——多个Activity相互交互的生命周期)