AndroidStudio-Activity的生命周期

一、Avtivity的启动和结束

从当前页面跳到新页面,跳转代码如下:

startActivity(new Intent(源页面.this,目标页面.class));

从当前页面回到上一个页面,相当于关闭当前页面,返回代码如下:

finish();// 结束当前的活动页面

示例:

在activity_act_start.xml文件中:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:id="@+id/btn_act_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳到下个页面"/>

</LinearLayout>

在ActStartActivity.java文件中:

java 复制代码
package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ActStartActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_start);
        findViewById(R.id.btn_act_next).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(this,ActFinishActivity.class));
    }
}

在activity_act_finsih.xml文件中:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="5dp"
        android:src="@drawable/ic_back"/>

    <Button
        android:id="@+id/btn_finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="完成"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按返回键,或者点击左上角的箭头图标,或者点击上面的完成按钮,均可关闭当前页面,返回上个页面"/>

</LinearLayout>

在ActFinishActivity.java文件中:

java 复制代码
package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class ActFinishActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_finish);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.btn_finish).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.iv_back || v.getId() == R.id.btn_finish){
            //结束当前的活动页面
            finish();
        }
    }
}

二、Activity的生命周期

示例:

java 复制代码
package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class ActStartActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ning";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"ActStartActivity onCreate");
        setContentView(R.layout.activity_act_start);
        findViewById(R.id.btn_act_next).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(this,ActFinishActivity.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"ActStartActivity onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"ActStartActivity onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"ActStartActivity onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"ActStartActivity onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"ActStartActivity onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG,"ActStartActivity onRestart");
    }
}

发布运行:

点击跳转:

发现点击跳转后onPause和onStop运行

点击返回后执行:

返回主页面后执行:

总结:

1.Activity 的生命周期:

(1)onCreate:创建活动。把页面布局加载进内存,进入了初始状态。

(2)onStart:开始活动。把活动页面显示在屏幕上,进入了就绪状态

(3)onResume:恢复活动。活动页面进入活跃状态,能够与用户正常交互,例如允许响应用户的点击动作、允许用户输入文字等等

(4)onPause:例如允许响应用户的点击动作、允许用户输入文字等等。

(5)onStop:无法与用户正常交互。

(6)onDestroy:销毁活动。回收活动占用的系统资源,把页面从内存中清

(7)onRestart:重启活动。重新加载内存中的页面数据。

(8)onNewlntent:重用已有的活动实例。

2.各状态之间的切换过程

打开新页面的方法调用顺序为:onCreate-onStart-onResume

关闭旧页面的方法调用顺序为:onPause→onStop→onDestroy

相关推荐
embrace991 小时前
【C语言学习】结构体详解
android·c语言·开发语言·数据结构·学习·算法·青少年编程
用户69371750013841 小时前
11.Kotlin 类:继承控制的关键 ——final 与 open 修饰符
android·后端·kotlin
用户0273851840261 小时前
【Android】LiveData的使用以及源码浅析
android·程序员
用户69371750013841 小时前
10.Kotlin 类:延迟初始化:lateinit 与 by lazy 的对决
android·后端·kotlin
正经教主2 小时前
【Git】Git06:Git 管理 Android 项目教程(含GitHub)
android·git
安卓理事人2 小时前
安卓多种通知ui更新的方式(livedata,rxjava,eventbus等)
android·ui·echarts
BS_Li2 小时前
【Linux系统编程】Ext系列文件系统
android·linux·ext系列文件系统
zhangphil2 小时前
Android宽高不均等Bitmap缩放为指定宽高FitCenter到正方形Bitmap,Kotlin
android·kotlin
别或许3 小时前
13.用户管理
android
q***96584 小时前
springboot3整合knife4j详细版,包会!(不带swagger2玩)
android·前端·后端