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

Android: Services should be stopped when exiting the app

    Details

    • Story Points:
      7
    • Sprint:
      2019 Sprint 9

      Description

      Summary:
      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.

      Note:
      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.)

      tiapp.xml

      <?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">
      		<services>
      			<service url="Service.js"/>
      		</services>
      	</android>
      </ti:app>
      

      ./Resources/Service.js

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

      ./Resources/app.js

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

      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,
      });
      window.open();
      

      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,
      	});
      	intent.addCategory(Ti.Android.CATEGORY_HOME);
      	intent.setFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK);
      	Ti.Android.currentActivity.startActivity(intent);
      });
      window.open();
      

      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",
      });
      Ti.Android.startService(intent);
       
      var window = Ti.UI.createWindow();
      window.addEventListener("close", function(e) {
      	Ti.Android.stopService(intent);
      });
      window.open();
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

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

                    Git Source Code