【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现

简介: 【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现

基本界面设置


创建Slice与对应xml

q5.png

BarAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class BarAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bar);
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

FooAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class FooAbilitySlice extends AbilitySlice{
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_foo);
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

ability_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
    <Text
        ohos:id="$+id:text_bar"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="The Page Is Bar!"
        ohos:text_size="40vp"
        />
</DirectionalLayout>

ability_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
    <Text
        ohos:id="$+id:text_foo"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="The Page Is Foo !"
        ohos:text_size="40vp"
        />
</DirectionalLayout>

设置主页面

MainAbility

package com.example.myapplication;
import com.example.myapplication.slice.FooAbilitySlice;
import com.example.myapplication.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(FooAbilitySlice.class.getName());
    }
}

q4.png

跳转设置


BarAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
public class BarAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bar);
            Text text = findComponentById(ResourceTable.Id_text_bar);
            text.setClickedListener(listener -> present(new FooAbilitySlice(),new Intent()));
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

FooAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
public class FooAbilitySlice extends AbilitySlice{
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_foo);
        Text text = findComponentById(ResourceTable.Id_text_foo);
        text.setClickedListener(listener -> present(new BarAbilitySlice(),new Intent()));
    }
    @Override
    public void onActive() {
        super.onActive();
    }
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

q3.png

重写生命周期方法


按下Crtl+o选择你需要重写的方法

q2.png

q1.png

打印HiLog日志


FooAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class FooAbilitySlice extends AbilitySlice{
    public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP,0x11102,"FooAbilitySlice");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_foo);
        Text text = findComponentById(ResourceTable.Id_text_foo);
        text.setClickedListener(listener -> present(new BarAbilitySlice(),new Intent()));
    }
    @Override
    public void onActive() {
        HiLog.info(loglabel,"[xrilang]FooAbilitySlice onActive start");
        super.onActive();
    }
    @Override
    protected void onInactive() {
        HiLog.info(loglabel,"[xrilang]FooAbilitySlice onInactive start");
        super.onInactive();
    }
    @Override
    protected void onBackground() {
        HiLog.info(loglabel,"[xrilang]FooAbilitySlice onBackground start");
        super.onBackground();
    }
    @Override
    public void onForeground(Intent intent) {
        HiLog.info(loglabel,"[xrilang]FooAbilitySlice onForeground start");
        super.onForeground(intent);
    }
    @Override
    protected void onStop() {
        HiLog.info(loglabel,"[xrilang]FooAbilitySlice onstop start");
        super.onStop();
    }
}

BarAbilitySlice

package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class BarAbilitySlice extends AbilitySlice {
    public static final HiLogLabel loglabel = new HiLogLabel(HiLog.LOG_APP,0x11103,"BarAbilitySlice");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bar);
            Text text = findComponentById(ResourceTable.Id_text_bar);
            text.setClickedListener(listener -> present(new FooAbilitySlice(),new Intent()));
    }
    @Override
    public void onActive() {
        HiLog.info(loglabel,"[xrilang]BarAbilitySlice onActive start");
        super.onActive();
    }
    @Override
    protected void onInactive() {
        HiLog.info(loglabel,"[xrilang]BarAbilitySlice onInactive start");
        super.onInactive();
    }
    @Override
    protected void onBackground() {
        HiLog.info(loglabel,"[xrilang]BarAbilitySlice onBackground start");
        super.onBackground();
    }
    @Override
    protected void onStop() {
        HiLog.info(loglabel,"[xrilang]BarAbilitySlice onStop start");
        super.onStop();
    }
    @Override
    public void onForeground(Intent intent) {
        HiLog.info(loglabel,"[xrilang]BarAbilitySlice onForeground start");
        super.onForeground(intent);
    }
}

q6.png

q5.png

q4.png

q3.png

可以在搜索处搜索tag、key、以及一些你想要的字符

分析HiLog日志



q2.png

q1.png

q1.png

相关文章
|
IDE JavaScript API
HarmonyOS开发第一步,熟知开发工具DevEco Studio
本文主要以常见的功能点作为概述希望可以帮助到学习HarmonyOS的开发者。
516 0
|
16天前
|
监控 IDE 开发工具
「Mac畅玩鸿蒙与硬件5」鸿蒙开发环境配置篇5 - 熟悉 DevEco Studio 界面
本篇将详细介绍 DevEco Studio 的界面布局和主要功能模块,帮助开发者熟悉开发环境,提高开发效率。通过了解各个界面区域,开发者可以顺利找到所需工具,流畅开展鸿蒙应用开发。
36 2
「Mac畅玩鸿蒙与硬件5」鸿蒙开发环境配置篇5 - 熟悉 DevEco Studio 界面
|
16天前
|
开发工具 开发者 git
「Mac畅玩鸿蒙与硬件4」鸿蒙开发环境配置篇4 - DevEco Studio 高效使用技巧
本篇将进一步介绍如何在 DevEco Studio 中高效使用各种功能,通过掌握快捷键、代码补全、调试工具等,帮助开发者在鸿蒙应用开发中大幅提升工作效率。
46 1
「Mac畅玩鸿蒙与硬件4」鸿蒙开发环境配置篇4 - DevEco Studio 高效使用技巧
|
16天前
|
开发工具 git 开发者
「Mac畅玩鸿蒙与硬件3」鸿蒙开发环境配置篇3 - DevEco Studio 插件安装与配置
本篇将专注于如何在 DevEco Studio 中安装和配置必要的插件,以增强开发功能和提升效率。通过正确配置插件,开发流程能够得到简化,开发体验也会更加顺畅。
54 1
「Mac畅玩鸿蒙与硬件3」鸿蒙开发环境配置篇3 - DevEco Studio 插件安装与配置
|
16天前
|
开发工具 iOS开发 开发者
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在 Mac 上安装 DevEco Studio
本篇将专注于如何在 Mac 上安装鸿蒙开发工具 DevEco Studio,确保开发环境能够顺利搭建。完成安装后,可以正式开始鸿蒙应用的开发工作。
56 1
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在 Mac 上安装 DevEco Studio
|
18天前
|
存储 监控 安全
开发者的黄金时代:原生鸿蒙应用市场的全生命周期服务
2024年10月22日,华为发布了HarmonyOS NEXT,标志着鸿蒙生态进入商用发展阶段。原生鸿蒙应用市场全面焕新,不仅在UI设计、互动体验和隐私安全机制上进行了重塑,还为开发者和用户提供了从开发到分发的全生命周期服务。通过统一上架、多端分发、隐私合规保障等措施,原生鸿蒙应用市场助力开发者实现高效、安全的应用开发与分发,为全球数亿鸿蒙用户带来更流畅、更安全的使用体验。
|
6月前
|
调度 UED 开发者
【鸿蒙软件开发】UIAbility组件概况、生命周期与启动模式
【鸿蒙软件开发】UIAbility组件概况、生命周期与启动模式
862 0
【鸿蒙软件开发】UIAbility组件概况、生命周期与启动模式
|
开发工具
(0.3)HarmonyOS鸿蒙开发工具DevEco Studio 模拟器使用
(0.3)HarmonyOS鸿蒙开发工具DevEco Studio 模拟器使用
255 0
(0.3)HarmonyOS鸿蒙开发工具DevEco Studio 模拟器使用
|
Java 开发工具
(0.2)HarmonyOS鸿蒙开发工具DevEco Studio工程文件目录结构
(0.2)HarmonyOS鸿蒙开发工具DevEco Studio工程文件目录结构
517 0
(0.2)HarmonyOS鸿蒙开发工具DevEco Studio工程文件目录结构