Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25962] Android: Ti.App.resume and Ti.App.pause Addition - Google has added Lifecycle Components (LifecycleOwner) specifically for this

GitHub Issuen/a
TypeImprovement
PriorityCritical
StatusClosed
ResolutionDuplicate
Resolution Date2018-10-29T23:14:09.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsandroid, parity, pause, resume, ti.app
ReporterDavid van de Meer
AssigneeJoshua Quick
Created2018-04-12T14:20:44.000+0000
Updated2018-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.

Comments

  1. David van de Meer 2018-04-13

    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:
       public class AppLifecycleListener implements LifecycleObserver {
       
           @OnLifecycleEvent(Lifecycle.Event.ON_START)
           public void onMoveToForeground() {
               // app moved to foreground
           }
       
           @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
           public void onMoveToBackground() {
              // app moved to background
           }
       }
       
       // register observer
       ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener());
       
  2. David van de Meer 2018-04-16

    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?
           // Lifecycles
           implementation 'android.arch.lifecycle:extensions:1.1.1'
           annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
       
    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.
  3. Joshua Quick 2018-04-16

    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() and TiApplication.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 the Ti.App.pause and Ti.App.resume events would be good.
  4. David van de Meer 2018-04-24

    @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 know
  5. Joshua Quick 2018-04-24

    I 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.
  6. David van de Meer 2018-05-09

    @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?
       var TiApplicationClass = require('org.appcelerator.titanium.TiApplication');
       
  7. Joshua Quick 2018-05-09

    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.
  8. David van de Meer 2018-06-18

    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?
  9. Joshua Quick 2018-06-18

    No. The new support libraries providing the lifecycle components will be in Titanium 7.3.0.
  10. David van de Meer 2018-06-19

    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
        Example:
        // require appPauseResumeModule
        var appPauseResume = require('appPauseResume');
        // run appPauseResume and add resume and pause callbacks
        appPauseResume({pause: function(){
                            Ti.API.info("appPauseResume - pause");
                        },
                        resume: function(){
                            Ti.API.info("appPauseResume - resume");
                        },
                        setIntervalTime: 1000,  // Optional - Default: 1000 miliseconds (1 second) 
        });
        
  11. Joshua Quick 2018-10-29

    This was implemented by [TIMOB-26424] to be released in 7.5.0. Closing this ticket as duplicate.

JSON Source