Android基础-Activity生命周期的总结

前言:最开始学这个的时候,我还在想为什么要学生命周期,为什么要去了解它呢,我只要知道怎么让一个界面跳转到另一个界面不就行了吗?,举个栗子,假如你是一名医生,有一天有病人来你这里治病,你不可能就告诉它你有病,或者说你没病吧。

一:本节知识点

在这里插入图片描述

二:Activity生命周期状态

图片来源与来自这里

在这里插入图片描述
就通过一张图可能不容易理解吧,那举个栗子吧
栗子:新建一个安卓项目,在后端添加以下代码(添加日志打印,重写生命周期相应方法)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MainAction","调用onCreeate()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("MainAction","调用onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("MainAction","调用onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("MainAction","调用onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("MainAction","调用onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("MainAction","调用onDestroy()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("MainAction","调用onRestart()");
    }
      //(点击跳转)点击事件,点击跳转到activity_son
    public void btn_onclick(View view) {
        //启动另一个action
        Intent  intent=new Intent(MainActivity.this,SonActivity.class);
        startActivity(intent);
    }

    public void finish(View view) {
        finish();
    }

运行程序,观看Logcat
在这里插入图片描述
为了更好的观察我们添加相应过滤
在这里插入图片描述
在这里插入图片描述
输入自己日志的Tag
在这里插入图片描述
在这里插入图片描述
①:启动程序(对应图上ActivityStarts)
在这里插入图片描述
②:点击切换到另一个界面
在这里插入图片描述
③:再返回原界面
在这里插入图片描述
④:退出程序
在这里插入图片描述

三:Activity的创建和配置

1:直接创建XML布局文件,然后添加标签
①:直接创建XML布局文件
在这里插入图片描述
②:然后在AndroidManifest.Xml中添加标签
在这里插入图片描述
标签名为".布局名"
在这里插入图片描述
完整名应该是:包名.布局名
在这里插入图片描述
由于上面已经加了包名所以可以省略
在这里插入图片描述
2:直接创建,自动生成标签
在这里插入图片描述
在这里插入图片描述

四:开启和关闭Activity

1:Intent(我们常说的意图)
PackageContext:应用程序包实现的上下文
CLS:要用于意图的组件类。
在这里插入图片描述
创建一个从MainActivity.this下跳转到SonActivity.class中去的意图,然后通过StartActivity方法启动意图
在这里插入图片描述
进入到SonActivity.class中
在这里插入图片描述
通过onCreate()方法进行创建然后按照Activity生命周期
在这里插入图片描述
onCreate()=>onStart()=>onResume()=>当前Activity显示。

2: finish();方法是关闭当前Activity
这里finish是直接在首窗体(启动窗体)里创建的点击事件,当用户点击时当前Activity就关闭了,由于是首窗体(启动窗体),所以用户点击时直接关闭程序。
在这里插入图片描述
3:整个项目代码详情
①:activity_main.xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn_onclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="13dp"
        android:onClick="btn_onclick"
        android:text="点击跳转"
        app:layout_constraintBottom_toTopOf="@+id/btn_OnClickfinish"
        app:layout_constraintStart_toStartOf="@+id/btn_OnClickfinish" />

    <Button
        android:id="@+id/btn_OnClickfinish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="291dp"
        android:onClick="finish"
        android:text="点击关闭程序"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

②:activity_son.xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".SonActivity"
    tools:ignore="NamespaceTypo">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="340dp"
        android:text="Hello Word!"
        android:textColor="#000"
        android:textSize="50sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

③:后端代码

package cn.cg.lifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {

    //由系统调用:回调
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MainAction","调用onCreeate()");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("MainAction","调用onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("MainAction","调用onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("MainAction","调用onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("MainAction","调用onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("MainAction","调用onDestroy()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("MainAction","调用onRestart()");
    }

    //(点击跳转)点击事件,点击跳转到activity_son
    public void btn_onclick(View view) {
        //启动另一个action
        Intent  intent=new Intent(MainActivity.this,SonActivity.class);
        startActivity(intent);
    }

    public void finish(View view) {
        finish();
    }
}
发布了51 篇原创文章 · 获赞 18 · 访问量 5368

猜你喜欢

转载自blog.csdn.net/weixin_42753193/article/details/105242669