Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16889] Android: Adding 'resume' event to Ti.Android.currentActivity does not work with SDK >= 3.2.0

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2014-08-19T17:58:41.000+0000
Affected Version/sn/a
Fix Version/sRelease 3.3.1, Release 3.4.0
ComponentsAndroid
LabelsSupportTeam, module_android, qe-manualtest
ReporterDavide Cassenti
AssigneePing Wang
Created2014-04-28T13:01:20.000+0000
Updated2015-02-26T17:24:38.000+0000

Description

Problem description

When adding the resume event to Ti.Android.currentActivity, the resume is not called when the app is resumed. This works well in SDK 3.1.3

Steps to reproduce

1. Use the code below 2. Run the app on Android 3. Hit the home button on the device 4. Restore the app: no alert is shown

Code to reproduce

Ti.Android.currentActivity.addEventListener('resume', function(e) {
	alert("resumed");
});

var win = Ti.UI.createWindow({
	backgroundColor: 'white'
});
win.open();

Note

The same code and steps work fine on SDK 3.1.3

Comments

  1. Mark Mokryn 2014-05-18

  2. Mark Mokryn 2014-05-19

    Currently, on windows it's possible to set win.activity.onCreateOptionsMenu = some function even before the win.open() call, and indeed this function will be called at the right time. So why not use a similar mechanism for the rest of the lifecycle events, instead of activity.addEventListener which is unavailable prior to the open event, which is too late for many of the events? Adding to this confusion is that for TabGroups, the open event is not too late to install the event listeners (and also setting tabGroup.activity.onCreateOptionsMenu prior to open will not work either....) To be honest, I don't think this should wait until after 3.3.0 - these are major issues.
  3. Mark Mokryn 2014-05-19

    See pull request: https://github.com/appcelerator/titanium_mobile/pull/5701 See test application below. Explanation: 1. Added onCreate, onStart, onResume, onRestart, onPause, onStop, and onDestroy properties to the tab group or window activity property. 2. For Window, you must set these properties prior to window.open() called. 3. For TabGroup, you can set these only after the open event (it's unfortunate that the two APIs are different in this, but that's outside the scope of this PR). 4. These callbacks are better than the events, since they're called synchronously, thus more reliable. Plus it makes more sense if onCreateOptionsMenu, onPrepareOptionsMenu, etc are using the same mechanism. 5. The pull request did not modify the event handling. These can still be used as before - but they're *BUGGY* for windows, as noted. 6. I recommend the lifecycle events be deprecated. The user can always fire events in javascript from the callbacks, if desired. 7. Docs for this TBD - pending Appcelerator approval of this PR.
       Titanium.UI.setBackgroundColor('#000');
       
       // create tab group
       var tabGroup = Titanium.UI.createTabGroup();
       
       var win1 = Titanium.UI.createWindow({  
           title:'Tab 1',
           backgroundColor:'#fff'
       });
       var tab1 = Titanium.UI.createTab({  
           icon:'KS_nav_views.png',
           title:'Tab 1',
           window:win1
       });
       
       var label1 = Titanium.UI.createLabel({
       	color:'#999',
       	text:'Click to open window',
       	font:{fontSize:20,fontFamily:'Helvetica Neue'},
       	textAlign:'center',
       	width:'auto'
       });
       
       function openWin() {
       	var childWin = Ti.UI.createWindow({});
       	// For windows we set the activity lifecycle callback prior to calling open()
       	// We cannnot call childWin.activity.addEventListener yet since the method is undefined
       	// And if we call childWin.activity.addEventListener during the open event
       	// we can miss the create, start, restart and resume events.
       	// This is device and timing dependent.
       	// Thus for windows we must define the callbacks here, and events should be deprecated.
       	childWin.activity.onCreateOptionsMenu = function(e){
       		Ti.API.info('childWin onCreateOptionsMenu called');
       	};	
       	childWin.activity.onStart = function(){
       		Ti.API.info('childWin onStart called');
       		// In the "onStart, etc callbacks this refers to the activity
       		Ti.API.info('apiName: ' + this.apiName);     
       	};
       	childWin.activity.onRestart = function(){
       		Ti.API.info('childWin onRestart called');
       	};
       	childWin.activity.onCreate = function(){
       		Ti.API.info('childWin onCreate called');
       	};
       	childWin.activity.onResume = function(){
       		Ti.API.info('childWin onResume called');
       	};
       	childWin.activity.onPause = function(){
       		Ti.API.info('childWin onPause called');
       	};
       	childWin.activity.onStop = function(){
       		Ti.API.info('childWin onStop called');
       	};
       	childWin.activity.onDestroy = function(){
       		Ti.API.info('childWin onDestroy called');
       	};
       	childWin.open();
       }
       label1.addEventListener('click', openWin);
       win1.add(label1);
       
       var win2 = Titanium.UI.createWindow({  
           title:'Tab 2',
           backgroundColor:'#fff'
       });
       var tab2 = Titanium.UI.createTab({  
           icon:'KS_nav_ui.png',
           title:'Tab 2',
           window:win2
       });
       
       var label2 = Titanium.UI.createLabel({
       	color:'#999',
       	text:'I am Window 2',
       	font:{fontSize:20,fontFamily:'Helvetica Neue'},
       	textAlign:'center',
       	width:'auto'
       });
       
       win2.add(label2);
       
       tabGroup.addTab(tab1);  
       tabGroup.addTab(tab2);  
       
       tabGroup.addEventListener('open', function() {
       	// The tabGroup.activity property is valid only after open
       	// we can set activity properties for synchronous callbacks
       	// or tabGroup.activity.addEventListener() will also work
       	tabGroup.activity.onCreateOptionsMenu = function(e){
       		Ti.API.info('tabGroup onCreateOptionsMenu called');
       	};	
       	tabGroup.activity.onStart = function(){
       		Ti.API.info('tabGroup onStart called');
       	};
       	tabGroup.activity.onRestart = function(){
       		Ti.API.info('tabGroup onRestart called');
       	};
       	tabGroup.activity.onCreate = function(){
       		Ti.API.info('tabGroup onCreate called');
       	};
       	tabGroup.activity.onResume = function(){
       		Ti.API.info('tabGroup onResume called');
       	};
       	tabGroup.activity.onPause = function(){
       		Ti.API.info('tabGroup onPause called');
       	};
       	tabGroup.activity.onStop = function(){
       		Ti.API.info('tabGroup onStop called');
       	};
       	tabGroup.activity.onDestroy = function(){
       		Ti.API.info('tabGroup onDestroy called');
       	};	
       });
       
       tabGroup.open();
       
  4. Mark Mokryn 2014-05-19

    And please - accept this for 3.3.0, this issue is a real pain...
  5. Mark Mokryn 2014-05-28

    Please note that the PR does not modify any existing APIs,and thus is completely safe to integrate for 3.3.0
  6. Vishal Duggal 2014-06-02

    Regarding the original testcase posted in the ticket, this is due to the Android window re-architecture done as part of TIMOB-13796. All windows by default are now heavyweight windows (hosted by their own activities). To get the same behavior as 3.1.3 in 3.2.X releases add the following property to tiapp.xml
       <property name="ti.android.useLegacyWindow" type="bool">true</property>
       
    Setting this property will open windows hosted by the root activity which is what the currentActivity variable points to in the sample posted. Note that this property has no effect in 3.3.0 and later releases since support for light weight windows has been dropped. So opening the window will pause the root activity and backgrounding and foregrounding the app once the window is open only causes the window activity to send out events (the root activity remains paused).
  7. Mark Mokryn 2014-06-03

    @Vishal: this ticket should be changed to "Activity.create, start, resume, restart don't work", and that's what the PR fixes. You can try the test case in my comment with the PR and you will see in doesn't work for Windows (just of course change the onCreate etc callbacks to "addEventListener"). If you addEventListener prior to Window open event the app will crash, and if you add it after the Window open you will miss some of events. This is actually very critical. Referring to 3.3.0+ of course.
  8. Terry Morgan 2014-06-16

    Are there any updates on this? I'm having similar problems trying to listen for pause & resume events on the tabGroup activity.
  9. Mark Mokryn 2014-07-07

    @Vishal, @Ingo, please merge the pull request, it works fine for me on 3.3.0, and Window activity events are outright broken without it. @Terry Morgan: try hooking the TabGroup activity events after the TabGroup open event. That should still work. It's Window Activity events that are hosed for now.
  10. Mark Mokryn 2014-07-18

  11. Mark Mokryn 2014-07-24

    PR updated to use async callbacks, please merge for master and 3.3.1, thanks.
  12. Ping Wang 2014-07-24

    Filed TIDOC-1788 for updating the doc. Resolved this ticket as Fixed.
  13. Mark Mokryn 2014-07-25

    Marked as resolved and fixed but still not merged for 3_3_X. I'm sure it can be merged automatically for that branch as well.
  14. Ping Wang 2014-07-25

    3_3_X PR: https://github.com/appcelerator/titanium_mobile/pull/5934
  15. Lokesh Choudhary 2014-08-19

    Verified the fix. The onCreate, onStart, onResume, onRestart, onPause, onStop, and onDestroy properties work as expected. Closing. Environment: Appc Studio : 3.4.0.201408051600 Ti SDK : 3.4.0.v20140815142514 Mac OSX : 10.8.5 Alloy : 1.5.0-dev CLI - 3.4.0-dev Code Processor: 1.1.1 Nexus 5 - android 4.4.4
  16. Mark Mokryn 2014-09-23

    Please update the fix for this with this small but important fix: https://github.com/appcelerator/titanium_mobile/pull/6160 This makes the lifecycle calls synchronous. Explanation: Strange things happen if the Android Activity lifecycle calls (onCreate, onResume, onCreateOptionsMenu, etc. ) are async: the calls into Javascript happen much later than they should - sometimes disastrously later. For example, if a window is created and opened as the screen is locked, Android will quickly cycle through create, start, resume, pause, stop - and I have seen window.activity.onResume, onCreateOptionsMenu etc called after the activity was stopped! This of course creates issues if the code tries to do something with the activity when the activity's state is inappropriate for the action. This should be merged into master as well as 3_4_X
  17. Mark Mokryn 2014-09-24

    This module requires the new PR for this ticket: https://github.com/mokesmokes/titanium-android-facebook
  18. Marcus Ilgner 2015-02-26

    Looks like there has been a regression for this ticket? Just tried converting from $.win.activity.onResume = ... to Ti.Android.currentActivity.addEventListener("resume", ...) and while the former works just fine, the second one doesn't.
  19. Mark Mokryn 2015-02-26

    Read the latest docs. Use the onResume property on the Activity, not the event listener.
  20. Marcus Ilgner 2015-02-26

    In that case, the documentation at http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Android.Activity should probably be updated to also not mention addEventListener in the example code.
  21. Mark Mokryn 2015-02-26

    Yup, however note that the list of properties and events is indeed correct.

JSON Source