Question

As reference, this is on Android, and this is the Titanium Forum Post

I'm noticing this issue with several applications I've done with Appcelerator, all of them: no matter if they are simple apps or complex apps, but is more frequent in larger apps.

For example, I'm working on an application that needs to notify its location (GPS location) every 5 minutes. The application works perfectly when working in foreground, however, every time I put the app on the background (if I minimize the app by using the Home button) the application just crashes and all the services are stopped.

For notifying the application, here's the flow I'm using:

  1. When I start the application, I start an Alarm, using the AlarmManager module.
  2. This AlarmManager, adds a new Alarm Service, which will be triggered every 5 minutes.
  3. Every 5 minutes, the AlarmManager starts the service and notifies the server with the current location.

This works great.

If I hit the "Home" button, the application just crashes (on the Application Manager) sometimes, but is more frequent when using other applications on the foreground while leaving this app on the background.

I thought this was because the main Activity was stopped, so I decided to maintain the main activity / task state by using android:alwaysRetainTaskState="true"

If I reopen the application, the application just restarts. This happens more on low-end devices than on high-end devices, which makes me think that this is a memory issue.

I have some questions:

  1. Is this the expected behaviour?
  2. If so, how can I mitigate this (maybe a native module/service?)?
  3. Is my current flow the best flow for doing this or is there a better approach?

Any thoughts are more than appreciated.

Was it helpful?

Solution

For answering my own question: After a lot of tests, we saw that the problem was more frequent on older devices.

By reviewing the components, elements and doing an extense memory research, we discovered that this actually has to do on how Android manages Applications and the memory.

If OS decides your application is consuming too much memory on the background, it's able to remove it by any time. No matter if it's executing an Alarm or not.

With devices with more memory (newer devices), the problem just disappears.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top