2

As far as I understand from this question and the official reference and guide, only onDestroy() is called, when my app gets removed from the recent apps list.

In my app I have a single activity which starts a service to play music. In the activity´s onDestroy(), the service is not even mentioned, but still I get an illegal argument exception thrown when my app is removed from the list, pointing to said service:

Parameter specified as non-null is null: 
method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter intent 
at com.example.soulfetch2.BordunService.onStartCommand(BordunService.kt)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3468)`

As in onDestroy(), the service is not even mentioned, there must be something else trying to access the service that is not existing anymore. Any idea what it could be?

If onPause() and/or onStop() also are called, is there a way to distinguish the removal case from the other cases in which those two are called?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
BraveSentry
  • 339
  • 4
  • 19

3 Answers3

2

When you open the recent apps onPause and onStop are called, so when your activity is swiped away only onDestroy is left in the lifecycle.

For your exception your onStartCommand method should have the following signature

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int) : Int

As per the documentation

This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.

Ge3ng
  • 1,875
  • 1
  • 24
  • 38
  • If I do it like that, how do I use extras? If I make the intent nullable in the signature, I run into problems there: `if (intent.hasExtra("Play"))` gives me "only ?. or !!. are allowed". For `if (intent?.hasExtra("Play"))` i get a type mismatch (expected `intent`, found `intent?`) and for `if (intent!!.hasExtra("Play"))` I get a nullpointer exception instead of the illegal argument. – BraveSentry Mar 08 '19 at 21:17
  • You have to handle the null. You could return early if the intent is null IE `intent ?: return Service.START_NOT_STICKY ` You should also look at the flags that are passed in because they will signify what is gong on. – Ge3ng Mar 08 '19 at 21:19
2

As in onDestroy(), the service is not even mentioned, there must be something else trying to access the service that is not existing anymore. Any idea what it could be?

In some phones, When the app is removed from recent apps, It's like going to setting and force close app. so it close all the services related to the app. That is why you faced such problem.

If onPause() and/or onStop() also are called, is there a way to distinguish the removal case from the other cases in which those two are called?

As long as i know there is no way to distinguish the removal case.

but still I get an illegal argument exception thrown when my app is removed from the list

For fixing this you can take a look at what Ge3ng posted. It will fix your problem.

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
  • In a service there is a callback `onTaskRemoved` that will be called when an app is swiped away by a user. – Ge3ng Mar 08 '19 at 21:20
  • What about when the system stop the application ? or the whole application terminated? @Ge3ng – Omid Heshmatinia Mar 08 '19 at 22:46
  • Then it isn't guaranteed to be called. `onTaskRemoved` is only guaranteed to be called if the user initiates it from the the recent apps list. – Ge3ng Mar 09 '19 at 06:12
0

You have a Service that returns START_STICKY from onStartCommand(). When you swipe the task from the recent tasks list Android kills the OS process hosting your activities and your Service.

Since you told Android (via START_STICKY) that you want your Service to be restarted in the event that Android kills it, Android complies with your request and restarts your Service. When it does this, it calls onStartCommand() with a null Intent.

NOTE: I assume that onDestroy() is not getting called at all on your activities. You can add logging to onDestroy() to see if that is true.

David Wasser
  • 93,459
  • 16
  • 209
  • 274