【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现
# NOTE05:PageAbility生命周期的呈现
基本界面设置
创建Slice与对应xml
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());
}
}
跳转设置
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);
}
}
重写生命周期方法
按下Crtl
+o
选择你需要重写的方法
打印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);
}
}
可以在搜索处搜索tag、key、以及一些你想要的字符
分析HiLog日志
版 权 声 明
作者:萌狼蓝天
QQ:3447902411(仅限技术交流,添加请说明方向)
转载请注明原文链接:https://www.cnblogs.com/zwj/p/HarmonyOS_edu_note_05_PageAbilityLifeCycle.html