Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-26538

Android: Services should be stopped when exiting the app


    • Story Points:
    • Sprint:
      2019 Sprint 9


      If you start a service and then back out of the app, the JavaScript runtime is kept alive due to the change we've made via TIMOB-9831 so that the JavaScript service code can still function. The problem with this is if you re-launch the app after doing the above, you'll be stuck at the splash screen and the "app.js" won't be re-executed. The reason is because Titanium only supports running 1 JavaScript runtime at a time. And we don't want to start a new JavaScript runtime instance since it wouldn't be able to communicate with the 1st runtime that is running the service... and we don't want the 2nd runtime to start a 2nd service instance.

      This is a design issue and needs to change.

      We should revert the change made by TIMOB-9831 and stop all services when backing out of the app. If an app developer wants a service to keep running when backing out, then they should set up the root window's exitOnClose property to false so that the UI and service will continue to run in the background as shown in "Work-Around 1" down below (or alternatively use "Work-Around 2" solution). This also makes the app work more like iOS.

      BroadcastReceiver has this same issue. If you don't unregister a BroadcastReceiver before exiting the app, then the next time you launch the app, you'll be stuck at the splash screen.

      Steps to reproduce:

      1. Set up a Classic Titanium app using the below code an XML settings.
      2. Build and run on Android.
      3. Press the back button to exit the app.
      4. Re-launch the app.
      5. Notice that you're stuck on the splash screen. (This is the issue.)


      <?xml version="1.0" encoding="UTF-8"?>
      <ti:app xmlns:ti="http://ti.appcelerator.org">
      	<android xmlns:android="http://schemas.android.com/apk/res/android">
      			<service url="Service.js"/>


      Ti.API.info("@@@ Service started.");


      // Start the service.
      var intent = Ti.Android.createServiceIntent({
      	url: "Service.js",
      // Display the main window.
      var window = Ti.UI.createWindow();

      Work-Around 1:
      For the 1st window you open, set it's exitOnClose property to false like the below. This prevents the back button from closing your root window and makes the app work more like iOS. It also allows the service to operate in the background without issue. This is the best solution.

      var window = Ti.UI.createWindow({
      	exitOnClose: false,

      Work-Around 2:
      Override the root window's "androidback" event and use an intent to home-out of the app. This is similar to the work-around 1 solution where the root window is not closed and the service will continue to run in the background. Again, this makes your app effectively work like iOS.

      var window = Ti.UI.createWindow();
      window.addEventListener("androidback", function(e) {
      	// If the back key was press, cancel it and go to the home-screen instead.
      	var intent = Ti.Android.createIntent({
      		action: Ti.Android.ACTION_MAIN,

      Work-Around 3:
      Stop the service when the root window is about to close and let everything terminate.

      var intent = Ti.Android.createServiceIntent({
      	url: "Service.js",
      var window = Ti.UI.createWindow();
      window.addEventListener("close", function(e) {


          Issue Links



              • Assignee:
                jquick Joshua Quick
                jquick Joshua Quick
                Gary Mathews
              • Watchers:
                5 Start watching this issue


                • Created:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    Sync status:
                    Last received:
                    Last sent:

                    Git Source Code