1.ActivityLifeCycle:测试生命周期的Activity 2.CommonActivity是一个普通的Activity 3.DialogActivity是对话框型的Activity
activity生命周期.gif
一、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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<Button
android:id="@+id/btn_task_activity"
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"/>
<Button
android:id="@+id/btn_dialog_activity"
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"/>
</android.support.constraint.ConstraintLayout>
2.CommonActivity布局:ac_task.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<Button
android:id="@+id/btn_start_self"
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"/>
<Button
android:id="@+id/button_start_others"
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"/>
</android.support.constraint.ConstraintLayout>
3.DialogActivity布局:ac_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<TextView
android:id="@+id/textView"
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"/>
</android.support.constraint.ConstraintLayout>
4.DialogActivity对话框主题
<activity
android:name=".DialogActivity"
android:label="DialogActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog">
</activity>
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328
4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg
转载于:https://www.cnblogs.com/toly-top/p/9781954.html