鸿蒙App 彻底退出应用和重启如何实现?

565 阅读1分钟

移动应用开发中经常需要“再按一次退出”或“应用内版本切换重启生效”等场景功能。那么在鸿蒙APP开发中该如何实现呢?

1.再按一次退出

onBackPress(): boolean | void { 
    let nowtime= Date.now(); 
    if(nowtime-this.backTime<1000){ const mContext = getContext(this) as 
       common.UIAbilityContext mContext.terminateSelf(); 
    }else { 
       this.backTime=nowtime; this.showToast("再按一次将退出当前应用")
    } return true; 
}

2.杀死APP再重启


const mContext = getContext(this) as common.UIAbilityContext
mContext.terminateSelf(()=>{
  let want: Want ={
    bundleName:'com.hh.hmmyh',
    abilityName:"EntryAbility"
  }
  mContext.startAbility(want).then(()=>{
    console.info("应用被启动");
  })
});

下一篇:LocalStorege使用-juejin.cn/post/735058…