【鸿蒙 HarmonyOS】page内界面跳转 ( Abilit

2021-12-02  本文已影响0人  雨田Android开发

同一Page内导航

当发起导航的AbilitySlice和导航目标的AbilitySlice处于同一个Page时,您可以通过present()方法实现导航。如下代码片段展示通过点击按钮导航到其他AbilitySlice的方法:

public class MainAbilitySlice extends AbilitySlice {

    //terminate
    private Button btnNext;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        btnNext = (Button) findComponentById(ResourceTable.Id_btn_next);
        btnNext.setClickedListener(listener ->{
            SecondAbilitySlice abilitySlice = new SecondAbilitySlice();
            Intent intent1 = new Intent();
            intent1.setParam("params","来啦");
            present(abilitySlice,intent1);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

在SecondAbilitySlice中进行接收数据

public class SecondAbilitySlice extends AbilitySlice {

    private Button btnBack;
    private Text text;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);
        text = (Text)findComponentById(ResourceTable.Id_text_second);

        if(intent!=null){
            String params = intent.getStringParam("params");
            if(!params.isEmpty()){
                text.setText(params);
            }
        }
        btnBack = (Button)findComponentById(ResourceTable.Id_btn_back) ;
        btnBack.setClickedListener(listener->{
            onBackPressed();
        });
    }
}

某些特定情况下 可能存在跳转即关闭跳转前PageAbilitySlice
跳转后代码添加:onBackPressed();

上一篇下一篇

猜你喜欢

热点阅读