• 1.安卓基础之Activity生命周期


    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>

    本文由张风捷特烈原创,转载请注明
    更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
    你的喜欢与支持将是我最大的动力

  • 相关阅读:
    ASP.NET Core: What I learned!
    Entity Framework Core with GraphQL and SQL Server using HotChocolate
    Angular 9 Chart.js with NG2-Charts Demo
    POST调用WCF方法-项目实践
    项目实战-登录速度优化笔记
    MP4视频流base64数据转成Blob对象
    使用Vue+ElementUI实现前端分页
    JS端实现图片、视频时直接下载而不是打开预览
    Dynamic CRM工作流流程实战
    Dynamic CRM插件调试与单元测试
  • 原文地址:https://www.cnblogs.com/toly-top/p/9781917.html
Copyright © 2020-2023  润新知