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

相关推荐
黄林晴27 分钟前
如何判断手机是否是纯血鸿蒙系统
android
火柴就是我34 分钟前
flutter 之真手势冲突处理
android·flutter
法的空间1 小时前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
循环不息优化不止1 小时前
深入解析安卓 Handle 机制
android
恋猫de小郭1 小时前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
jctech1 小时前
这才是2025年的插件化!ComboLite 2.0:为Compose开发者带来极致“爽”感
android·开源
用户2018792831671 小时前
为何Handler的postDelayed不适合精准定时任务?
android
叽哥2 小时前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
Cui晨2 小时前
Android RecyclerView展示List<View> Adapter的数据源使用View
android
氦客2 小时前
Android Doze低电耗休眠模式 与 WorkManager
android·suspend·休眠模式·workmanager·doze·低功耗模式·state_doze