[TIMOB-25962] Android: Ti.App.resume and Ti.App.pause Addition - Google has added Lifecycle Components (LifecycleOwner) specifically for this
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | Critical |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2018-10-29T23:14:09.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Android |
Labels | android, parity, pause, resume, ti.app |
Reporter | David van de Meer |
Assignee | Joshua Quick |
Created | 2018-04-12T14:20:44.000+0000 |
Updated | 2018-10-29T23:14:09.000+0000 |
Description
This is a Feature request to add Ti.App.resume and Ti.App.pause event to Android.
Google recently added [Lifecycle Components](https://developer.android.com/topic/libraries/architecture/lifecycle.html) and more specifically [LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html) that has been designed specifically for this.
From the Lifecycle Owner Docs:
"A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment."
There is also a very good Blog post with examples and exactly what is needed here:
https://proandroiddev.com/detecting-when-an-android-app-backgrounds-in-2018-4b5a94977d5c
It would be great if we could add this to the core to finally have Ti.App.resume and Ti.App.pause events and not have to count on all kinds of hacks, tools, modules etc.
I will be working on getting this setup in my test project with Hyperloop and report back with a working example.
The actual Class Google has created for this is [ProcessLifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner.html) From the Docs: Class that provides lifecycle for the whole application process. "You can consider this LifecycleOwner as the composite of all of your Activities, except that ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched. Other lifecycle events will be dispatched with following rules: ProcessLifecycleOwner will dispatch ON_START, ON_RESUME events, as a first activity moves through these events. ON_PAUSE, ON_STOP, events will be dispatched with a delay after a last activity passed through them. This delay is long enough to guarantee that ProcessLifecycleOwner won't send any events if activities are destroyed and recreated due to a configuration change. {color:#14892c}*It is useful for use cases where you would like to react on your app coming to the foreground or going to the background and you don't need a milliseconds accuracy in receiving lifecycle events.*{color}" I spent some time trying to get this to work with hyperloop but I was unable. It should be as simple as:
Hi, I am willing to put in the work to get this implemented but I will need some advice from some of the main android devs on how they want it to be done. 1. We will need to add dependencies - how would we add these?
2. From what I can see from the current SDK code we have 2 options to implement this - What would be the best or preferred way? Option 1: Add to [onCreate](https://github.com/appcelerator/titanium_mobile/blob/b758504ce898d0e93e461849cbb8975283da85bc/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java#L360) of Android app - with triggers that fire but only fire to the app level js if listeners are added This would match what iOS does via [registerForNotifications](https://github.com/appcelerator/titanium_mobile/blob/8ab7ac87ef1663c4cd8b50afc59b3bea72f31d53/iphone/Classes/TiModule.m#L114) Option 2: Add to [onHasListenersChanged](https://github.com/appcelerator/titanium_mobile/blob/b758504ce898d0e93e461849cbb8975283da85bc/android/modules/app/src/java/ti/modules/titanium/app/AppModule.java#L255) as was done when adding listeners for "accessibilitychanged". Again I am not sure about this and there could be other better ways to do this, but I am willing to do the work, test etc to get this done, I would just need some guidance from one of the main Android devs.
We already have the ability to detect if a Titanium app is in the foreground/background on the Java side now. Internally, we monitor it via our
TiApplication.addActivityTransitionListener()
andTiApplication.isCurrentActivityInForeground()
. We do this by tracking the state of all Titanium derived activities, which was your only option before Google's offered the ability to monitor activity lifecycles globally. For example, we do this with our Titanium Gesture module so that it'll disable the accelerometer and gyroscope sensors when the app is backgrounded and re-enable them when put back into the foreground... [GestureModule.java#L80](https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/gesture/src/java/ti/modules/titanium/gesture/GestureModule.java#L80) The only goofy thing on Android that I recall is that if you press the power button while your app is in the foreground and then press the power button again to show the screen lock, the app's activity will be resumed even though the screen lock is still completely covering up the app. This is okay for normal UI apps, but any apps that play music/sound will typically still want it paused until the screenlock has been slid off. So, this could involve monitoring the screen lock via a BroadcastReceiver too... which is something Google's Lifecycle classes don't handle (last I tried it at least). I agree that supporting theTi.App.pause
andTi.App.resume
events would be good.@Joshua Quick - So at this stage it does not seem like we are able to use the new Google global activity lifecycle monitors via Hyperloop, I think it would need to be added to the core. Would using Hyperloop with
TiApplication.isCurrentActivityInForeground
be a good option for monitoring app in foreground / background at this stage? Or what do you suggest? Like I said I am willing to put in the work to get the new proper Google Global monitors added to the core but I would need some guidance on what method to use to add this etc. Let me knowI don't think we need to add this library. We already have the APIs needed internally to do this. We'll look into this later.
@Rene Pot @Joshua Quick - Would using Hyperloop with
TiApplication.isCurrentActivityInForeground
be a good option for now until this is part of the core? I have tried to get that class imported with hyperloop using the code below but have not been able to - any suggestions?Unfortunately, hyperloop is not set up to access our core Titanium libraries. So, this isn't going to work. Unless you do it via Java reflection in hyperloop, but that would be kind of nuts. :P For your info, we plan on updating our Google Support libraries in Titanium 7.2.0... and the newest libraries "requires" the lifecycle components as a dependency. So, perhaps you should wait until then if you can.
Hi @Joshua Quick - I see that Titanium 7.2.0. was released, does this contain the new Google Support libraries that could help us do this? If so how do we proceed at making this simpler and more possible?
No. The new support libraries providing the lifecycle components will be in Titanium 7.3.0.
For now I have built and released and Hyperloop Module for this: https://github.com/dieskim/Appcelerator.Hyperloop.appPauseResume Cross-platform Hyperloop Module for detecting if app paused or resumed on both the iOS and Android - iOS uses core T.App EventListeners pause and resume - Android uses Narive Java Classes via Hyperloop and a setInterval to check and detect
This was implemented by [TIMOB-26424] to be released in 7.5.0. Closing this ticket as duplicate.