卡片跳轉快應用指定頁面,如何點返回直接退出快應用回到卡片


問題現象:在快應用已經打開A頁面的情況下,此時若從卡片(或其他媒介)跳轉至快應用指定頁面B,點擊左上角返回鍵,退出頁面順序是B-A-卡片,無法一鍵直接返回卡片(或其他媒介)。

需要實現的場景:在快應用已經打開A頁面的情況下,從卡片(或其他媒介)跳轉至快應用指定頁面B,點擊左上角返回鍵能夠一鍵退出快應用,直接返回卡片(或其他媒介)。

問題分析

上述的問題現象是由於頁面采用了默認的啟動模式standard,"standard"模式時會每次打開新的目標頁面(多次打開目標頁面地址時會存在多個相同頁面),導致頁面棧中會依次緩存頁面A,B,退出時頁面會依次出棧,無法一鍵返回。這里建議用戶從卡片跳轉至快應用時,使用動態聲明的方式指定頁面B的啟動模式為clearTask即可解決。指定clearTask時,會直接清除原先的頁面A,打開頁面B,如此頁面棧中只有頁面B存在,返回時即可直接退出快應用。

解決方法

卡片跳轉快應用示例代碼(采用deeplink鏈接):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
< html >
< head >
   < meta  charset = "UTF-8" >
   < meta  name = "viewport"  content = "width=device-width, initial-scale=1.0" >
   < meta  http-equiv = "X-UA-Compatible"  content = "ie=edge" >
   < title >快應用測試</ title >
</ head >
< body >
   < a  href = "hwfastapp://com.huawei.hello1/Test?___PARAM_LAUNCH_FLAG___=clearTask" >使用hwfastapp打開</ a >
   < br >
   < br >
   < a  href = "hap://app/com.huawei.hello1/Test?___PARAM_LAUNCH_FLAG___=clearTask" >使用hap打開</ a >
</ body >
</ html >

卡片跳轉的快應用目標頁面(根據當前頁面棧的數量可以發現始終只有一個頁面):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
< template >
   < div  class = "container" >
     < text >___PARAM_LAUNCH_FLAG___=</ text >
     < text >{{taskflag}}</ text >
     < text >當前頁面棧的數量</ text >
     < text >{{length}}</ text >
   </ div >
</ template >
  
< style >
   .container {
     flex-direction: column;
     align-content: center;
     align-items: center;
     justify-content: center;
   }
  
   text {
     margin-bottom: 50px;
   }
</ style >
  
< script >
import router from '@system.router';
   export default {
     data: {
       // The default is the local app internal image
       photoUri: '/Common/logo.png',
       taskflag:'',
       PARAM_LAUNCH_FLAG:'',
       length:''
     },
  
     onInit() {
       this.$page.setTitleBar({ text: 'deepLink' })
       var that=this;
       that.taskflag=this.PARAM_LAUNCH_FLAG;
       // 調用getPages方法
       let pages = router.getPages()
       // 由於獲得的值是一個JSON數組,所以直接打印是打印不出來的,可以使用下面的方法來打印
       console.log("tag", this.printJSONArray(router.getPages()));
       that.length= router.getLength();
       console.log("pages' length = "+that.length);
     },
  
     printJSONArray(array) {
       let result = ""
       const suffix = ", "
       Array.isArray(array) && array.forEach((element, index) => {
         result = result + JSON.stringify(element) + (index === array.length-1 ? "" : ", ")
         })
       return result
     }
   }
</ script >

建議與總結

1. 頁面啟動模式有兩種配置方式,一種是在manifest文件中靜態聲明,一種是動態傳參聲明,建議使用動態模式,根據自己需要進行配置,靜態聲明的方式適用於單一固定場景,無法靈活調整。

參見文檔:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-startupmode

2.deeplink文檔:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-develop-deeplink

 

原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0201441178593350400?fid=18

原作者:Mayism


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM