2016-12-29 63 views
2

我想在用戶離開應用程序時執行方法。我什麼都試過:當用戶離開應用程序時,Ionic 2執行方法

ionViewWillUnload() { 
    console.log("Wlill unload"); 
    this.leaveRoom(); 
} 

onDestroy() { 
    console.log("DESTROY"); 
    this.leaveRoom(); 
} 

ionViewWillLeave() { 
    this.leaveRoom(); 
} 

偏偏當用戶關閉應用程序或當用戶刷新頁面,他們不會執行。

有什麼想法?

回答

10
  1. 進口平臺:

    import { Platform } from 'ionic-angular';

  2. 添加平臺的constuctor:

    constructor(public navCtrl: NavController, platform: Platform)

  3. 訂閱平臺pauseresume

     platform.ready().then(() => { 
         this.platform.pause.subscribe(() => { 
          console.log('[INFO] App paused'); 
         }); 
    
         this.platform.resume.subscribe(() => { 
          console.log('[INFO] App resumed'); 
         }); 
        }); 
    
+0

upVote/downVote,mark解決/標記未解決,現在你自己回答,接下來是什麼?而你只是使用我提到的方法。 – avilac

+0

我把它標記爲已解決,因爲雖然它有效,但它沒有。您需要訂閱平臺暫停和恢復。而不是編輯所有的答案,我創建了一個新的 – TheUnreal

+0

當應用程序關閉時,「暫停」事件不會在IOS上觸發。只有當它放在後臺。 –

0

我想你在尋找可以在這裏找到:

http://cordova.apache.org/docs/en/6.x/cordova/events/events.html#endcallbutton

特別是本次活動:

function onEndCallKeyDown() { 
    // Handle the end call button 
} 

爲了使您必須首先聲明這個監聽事件工作:

document.addEventListener("endcallbutton", onEndCallKeyDown, false); 

的onPause事件:

document.addEventListener("pause", onPause, false); 

function onPause() { 
    // Handle the pause event 
} 

的onResume事件

document.addEventListener("resume", onResume, false); 

function onResume() { 
    // Handle the resume event 
} 

希望它能幫助!

+0

此事件僅支持'blackberry10'。 – TheUnreal

+0

我編輯了答案,現在它指向實際的科爾多瓦版本。 – avilac

+0

@TheUnreal在這裏您可以獲得爲新版本更新的受支持平臺列表:http://cordova.apache.org/docs/en/6.x/guide/support/index.html – avilac

相關問題