Application crashes after a period in the background (including services)

You must Login before you can answer or comment on any questions.

As reference, this is on Android.

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.

Is this the expected behaviour? If so, how can I mitigate this? Is my current flow the best flow for doing this or is there a better approach?

Any thoughts are more than appreciated.

— asked 12 months ago by Demostenes Garcia
3 Comments
  • Hello,

    Android OS will kill any app in the background if needed. There is no guarantee of multitask or multiple apps running at the same time. If you restart your phone and don't start other stuff, the app will live longer. If you are using a lot of programs, it's likely that your app will be killed.

    In the other hand, what do you mean the "app crashes"? What do you see in the logs?

    Best,

    Mauro

    — commented 12 months ago by Mauro Parra

  • Hi Mauro,

    I noticed exactly the same. After a fresh start, the app keeps working for a longer period of time.

    However, this is the issue I notice after a while (more likely when using other apps):

    08-28 21:15:08.056: E/TiApplication(2133): java.lang.NullPointerException
    08-28 21:15:08.056: E/TiApplication(2133):  at org.appcelerator.titanium.TiApplication.postAnalyticsEvent(TiApplication.java:690)
    08-28 21:15:08.056: E/TiApplication(2133):  at ti.modules.titanium.geolocation.TiLocation.doAnalytics(TiLocation.java:155)
    08-28 21:15:08.056: E/TiApplication(2133):  at ti.modules.titanium.geolocation.GeolocationModule.doAnalytics(GeolocationModule.java:230)
    08-28 21:15:08.056: E/TiApplication(2133):  at ti.modules.titanium.geolocation.GeolocationModule.eventListenerAdded(GeolocationModule.java:534)
    08-28 21:15:08.056: E/TiApplication(2133):  at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1049)
    08-28 21:15:08.056: E/TiApplication(2133):  at ti.modules.titanium.geolocation.GeolocationModule.handleMessage(GeolocationModule.java:224)
    08-28 21:15:08.056: E/TiApplication(2133):  at android.os.Handler.dispatchMessage(Handler.java:126)
    08-28 21:15:08.056: E/TiApplication(2133):  at android.os.Looper.loop(SourceFile:351)
    08-28 21:15:08.056: E/TiApplication(2133):  at android.app.ActivityThread.main(ActivityThread.java:3820)
    08-28 21:15:08.056: E/TiApplication(2133):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-28 21:15:08.056: E/TiApplication(2133):  at java.lang.reflect.Method.invoke(Method.java:538)
    08-28 21:15:08.056: E/TiApplication(2133):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    08-28 21:15:08.056: E/TiApplication(2133):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    08-28 21:15:08.056: E/TiApplication(2133):  at dalvik.system.NativeStart.main(Native Method)
    Does it makes any sense?

    — commented 12 months ago by Demostenes Garcia

  • This is another error after a crash:

    08-29 21:11:21.944: E/TiApplication(18259): (main) [529,790068] Sending event: exception on thread: main msg:java.lang.NullPointerException; Titanium 3.1.2,2013/08/14 12:46,5ceaff8
    08-29 21:11:21.944: E/TiApplication(18259): java.lang.NullPointerException
    08-29 21:11:21.944: E/TiApplication(18259):     at android.view.ViewConfiguration.get(ViewConfiguration.java:263)
    08-29 21:11:21.944: E/TiApplication(18259):     at android.view.View.<init>(View.java:1913)
    08-29 21:11:21.944: E/TiApplication(18259):     at android.view.ViewGroup.<init>(ViewGroup.java:312)
    08-29 21:11:21.944: E/TiApplication(18259):     at ti.modules.titanium.ui.widget.TiImageView.<init>(TiImageView.java:63)
    08-29 21:11:21.944: E/TiApplication(18259):     at ti.modules.titanium.ui.widget.TiUIImageView.<init>(TiUIImageView.java:94)
    08-29 21:11:21.944: E/TiApplication(18259):     at ti.modules.titanium.ui.ImageViewProxy.createView(ImageViewProxy.java:43)
    08-29 21:11:21.944: E/TiApplication(18259):     at org.appcelerator.titanium.proxy.TiViewProxy.handleGetView(TiViewProxy.java:452)
    08-29 21:11:21.944: E/TiApplication(18259):     at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:209)
    08-29 21:11:21.944: E/TiApplication(18259):     at android.os.Handler.dispatchMessage(Handler.java:126)
    08-29 21:11:21.944: E/TiApplication(18259):     at android.os.Looper.loop(SourceFile:351)
    08-29 21:11:21.944: E/TiApplication(18259):     at android.app.ActivityThread.main(ActivityThread.java:3820)
    08-29 21:11:21.944: E/TiApplication(18259):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-29 21:11:21.944: E/TiApplication(18259):     at java.lang.reflect.Method.invoke(Method.java:538)
    08-29 21:11:21.944: E/TiApplication(18259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    08-29 21:11:21.944: E/TiApplication(18259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    08-29 21:11:21.944: E/TiApplication(18259):     at dalvik.system.NativeStart.main(Native Method)
    08-29 21:11:21.960: E/AndroidRuntime(18259): FATAL EXCEPTION: main
    08-29 21:11:21.960: E/AndroidRuntime(18259): java.lang.NullPointerException
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.view.ViewConfiguration.get(ViewConfiguration.java:263)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.view.View.<init>(View.java:1913)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.view.ViewGroup.<init>(ViewGroup.java:312)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at ti.modules.titanium.ui.widget.TiImageView.<init>(TiImageView.java:63)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at ti.modules.titanium.ui.widget.TiUIImageView.<init>(TiUIImageView.java:94)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at ti.modules.titanium.ui.ImageViewProxy.createView(ImageViewProxy.java:43)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at org.appcelerator.titanium.proxy.TiViewProxy.handleGetView(TiViewProxy.java:452)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:209)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.os.Handler.dispatchMessage(Handler.java:126)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.os.Looper.loop(SourceFile:351)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at android.app.ActivityThread.main(ActivityThread.java:3820)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at java.lang.reflect.Method.invokeNative(Native Method)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at java.lang.reflect.Method.invoke(Method.java:538)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    08-29 21:11:21.960: E/AndroidRuntime(18259):    at dalvik.system.NativeStart.main(Native Method)
    08-29 21:11:22.088: E/AEE/LIBAEE(198): read_cmdline:com.threetech.tracingapp
    08-29 21:11:22.090: E/AEE/AED(91): jni/../aed/aed_trace.c, 158, No such file or directory
    Any help is more than appreciated.

    Thanks in advance.

    — commented 12 months ago by Demostenes Garcia

2 Answers

Seems like a bug to me.It happens also with Ti SDK 3.2.0. I posted it as https://jira.appcelerator.org/browse/TC-3693

Looks like I have the same issue, I'm using a background service, and here's my backtrace

06-11 14:24:34.014: E/TiApplication(21142): (main) [41115,41712] Sending event: exception on thread: main msg:java.lang.RuntimeException: Unable to resume activity {com.idfuze.prestatech/org.appcelerator.titanium.TiTranslucentActivity}: java.lang.NullPointerException; Titanium 3.2.3,2014/04/22 10:17,b958a70
06-11 14:24:34.014: E/TiApplication(21142): java.lang.RuntimeException: Unable to resume activity {com.idfuze.prestatech/org.appcelerator.titanium.TiTranslucentActivity}: java.lang.NullPointerException
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2826)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-11 14:24:34.014: E/TiApplication(21142):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 14:24:34.014: E/TiApplication(21142):     at android.os.Looper.loop(Looper.java:136)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.main(ActivityThread.java:5102)
06-11 14:24:34.014: E/TiApplication(21142):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 14:24:34.014: E/TiApplication(21142):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 14:24:34.014: E/TiApplication(21142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-11 14:24:34.014: E/TiApplication(21142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-11 14:24:34.014: E/TiApplication(21142):     at dalvik.system.NativeStart.main(Native Method)
06-11 14:24:34.014: E/TiApplication(21142): Caused by: java.lang.NullPointerException
06-11 14:24:34.014: E/TiApplication(21142):     at org.appcelerator.titanium.TiApplication.postAnalyticsEvent(TiApplication.java:690)
06-11 14:24:34.014: E/TiApplication(21142):     at org.appcelerator.titanium.TiBaseActivity.onResume(TiBaseActivity.java:1045)
06-11 14:24:34.014: E/TiApplication(21142):     at org.appcelerator.titanium.TiActivity.onResume(TiActivity.java:35)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1215)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.Activity.performResume(Activity.java:5327)
06-11 14:24:34.014: E/TiApplication(21142):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2811)
06-11 14:24:34.014: E/TiApplication(21142):     ... 12 more

Your Answer

Think you can help? Login to answer this question!