java activity生命周期_1.安卓基础之Activity生命周期
1.ActivityLifeCycle:测试生命周期的Activity2.CommonActivity是一个普通的Activity3.DialogActivity是对话框型的Activity一、Activity1.ActivityLifeCycle.javapublic class ActivityLifeCycle extends AppCompatActivity {private stati
1.ActivityLifeCycle:测试生命周期的Activity
2.CommonActivity是一个普通的Activity
3.DialogActivity是对话框型的Activity
一、Activity
1.ActivityLifeCycle.java
public class ActivityLifeCycle extends AppCompatActivity {
private static final String TAG = "ActivityLifeCycle";
@BindView(R.id.btn_task_activity)
Button mBtnTaskActivity;
@BindView(R.id.btn_dialog_activity)
Button mBtnDialogActivity;
@Override//Activity第一次创建时调用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_lifecycle);
ButterKnife.bind(this);
Log.e(TAG, "onCreate: ");
Log.e("TASK_ID", "Task id is " + getTaskId());
}
@Override//Activity可见的时候调用
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart: ");
}
@Override//Activity位于栈顶,获取焦点时调用
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: ");
}
@Override//Activity移除栈顶,失去焦点时调用
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: ");
}
@Override//由停止状态变为运行状态之前调用
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart: ");
}
@Override//Activity不可见的时候调用
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: ");
}
@Override//Activity销毁的时候调用
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: ");
}
@OnClick({R.id.btn_task_activity, R.id.btn_dialog_activity})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_task_activity:
//跳转到TaskActivity
startActivity(new Intent(this, TaskActivity.class));
break;
case R.id.btn_dialog_activity:
//跳转到DialogActivity
startActivity(new Intent(this, DialogActivity.class));
break;
}
}
}
2.CommonActivity.java
public class CommonActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_task);
ButterKnife.bind(this);
}
}
3.DialogActivity
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_dialog);
}
}
二、生命周期测试结果:
1.打开:onCreate==>onStart==>onResume
E/ActivityLifeCycle: onCreate:
E/ActivityLifeCycle: onStart:
E/ActivityLifeCycle: onResume:
2.跳转普通Activity:onPause==>onStop
E/ActivityLifeCycle: onPause:
E/ActivityLifeCycle: onStop:
3.从跳转的Activity返回:onRestart==>onStart==>onResume
E/ActivityLifeCycle: onRestart:
E/ActivityLifeCycle: onStart:
E/ActivityLifeCycle: onResume:
4.跳转到对话框Activity:onPause
E/ActivityLifeCycle: onPause:
5.从对话框Activity返回:onResume
E/ActivityLifeCycle: onResume:
6.旋转屏幕(相当于关闭再开启):onPause==>onStop==>onDestroy==>onCreate==>onStart==>onResume
E/ActivityLifeCycle: onPause:
E/ActivityLifeCycle: onStop:
E/ActivityLifeCycle: onDestroy:
E/ActivityLifeCycle: onCreate:
E/ActivityLifeCycle: onStart:
E/ActivityLifeCycle: onResume:
7.关闭:onPause==>onStop==>onDestroy
E/ActivityLifeCycle: onPause:
E/ActivityLifeCycle: onStop:
E/ActivityLifeCycle: onDestroy:
附录、布局:
1.ActivityLifeCycle布局:ac_lifecycle.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="打开TaskActivity"
android:textAllCaps="false"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="30dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="打开DialogActivity"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.873"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
2.CommonActivity布局:ac_task.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="打开自己"
android:textAllCaps="false"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="30dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="打开别人"
app:layout_constraintStart_toEndOf="@+id/btn_start_self"
app:layout_constraintTop_toTopOf="parent"/>
3.DialogActivity布局:ac_dialog.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A01_DialogActivity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="233dp"
tools:ignore="MissingConstraints"/>
4.DialogActivity对话框主题
android:label="DialogActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog">
更多推荐
所有评论(0)