Activity_B onPause --> Activity_A onRestart --> Activity_A onStart --> Activity_B onResume --> Activity_B onStop --> Activity_B onDestroy
跳转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)); } }
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>
<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>