2

Day9 鸿蒙,Ability全家桶(二)一步步实现AbilitySlice间导航

 3 years ago
source link: https://my.oschina.net/u/3534064/blog/4815860
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Day9 鸿蒙,Ability全家桶(二)一步步实现AbilitySlice间导航

昨天更新完DevEco Studio后,新建项目必须填入node.js的路径,本想着官网直接下载更新,无奈速度实在太慢了!(文章最后附录在mac上安装node.js的步骤)

我们今天接着介绍来介绍一下Page Ability的AbilitySlice间导航

首先创建工程会默认创建一个Ability和一个AbilitySlice,这里要验证实现AbilitySlice间导航,则需要新增一个AbilitySlice。见下图

步骤过程

(1)创建AbilitySlice的布局XML文件,在resources/base/layout路径下创建如ability_second.xml,具体内容可参考ability_main.xml;

(2)创建AbilitySlice的java文件,如在java/包名/slice路径下创建如SecondAbilitySlice.java,并将第一步创建的XML布局文件添加到Slice中去:

[@Override](https://my.oschina.net/u/1162528)
	
public void onStart(Intent intent) {
	
    super.onStart(intent);
			
    super.setUIContent(ResourceTable.Layout_ability_second);
			
}

(3)这个时候其实SecondAbilitySlice还未添加到MainAbility,需在MainAbility中添加入口:

    super.setMainRoute(MainAbilitySlice.class.getName());
			
    //添加SecondAbilitySlice的入口
			
    addActionRoute("action.second", SecondAbilitySlice.class.getName());

(4)步骤(3)中的定义的“action.second”还需要添加到config.json中进行配置,这个时候基本的AbilitySlice已经添加完毕:

    "actions": [
			
          "action.system.home",
						
          "action.second"
						
    ]

(5)同一Page内导航,可以通过以下方式实现:

   present(new SecondAbilitySlice(),new Intent());
		 
   //这里的0则是会通过onResult(int requestCode, Intent resultIntent)回调,检查requestCode
		 
   presentForResult(new SecondAbilitySlice(),new Intent(),0); 

【小知识点】

在mac上安装node.js的步骤

(1)安装brew:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

(2)验证brew是否安装:

brew -v

(3)安装node.js:

brew link node

brew uninstall node

brew install node

(4)验证node.js是否安装:

下发命令npm -v、node -v,能正确显示版本号即表示node安装成功,如果是通过homebrew安装的,下发命令brew list会显示node

本文由GZH程序员小小叶发布!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK