Skip to content

Instantly share code, notes, and snippets.

@balanza
Forked from kristjanmik/ti.android.events.js
Last active March 10, 2017 14:59
Show Gist options
  • Save balanza/5fbd1253b65c36eb2e42 to your computer and use it in GitHub Desktop.
Save balanza/5fbd1253b65c36eb2e42 to your computer and use it in GitHub Desktop.
A proper way to handle pause and resume event in titanium for android
/**
* A little workaround to proper handle resume and paused events on Android
*/
var platformTools = require('bencoding.android.tools').createPlatform();
var wasInForeGround = true;
//to be executed on every activity staus change
//check if it's passing from foreground to background or viceversa
function checkStatusSwitch() {
var isInForeground = platformTools.isInForeground();
if (wasInForeGround !== isInForeground) {
Ti.App.fireEvent(isInForeground ? 'resume' : 'paused');
wasInForeGround = isInForeground;
}
}
var win = Ti.UI.createWindow({});
win.addEventListener("open", function(e) {
win.activity.addEventListener("resume", function() {
checkStatusSwitch();
});
//Notice the pause event
win.activity.addEventListener("pause", function() {
checkStatusSwitch();
});
});
//Now works perfectly
Ti.App.addEventListener('resume',function(){
})
Ti.App.addEventListener('paused',function(){
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment