Android Question Android 8.x crashes due to Starter service not running

Computersmith64

Well-Known Member
Licensed User
Longtime User
I've noticed that one of my apps is experiencing a lot of crashes on Android 8.x devices lately. Looking at the crash reports I narrowed down the cause of the crash to a reference to a variable declared in the Starter service - however the OS appears to be stopping the service when the app is in the background.

I thought I could get around it by adding this code right at the start of my Activity_Create sub:

B4X:
If IsPaused(Starter) Then StartService(Starter)

My thought was that if the app had been in the background & Starter was stopped, this would get Starter running again before I referenced the variable in it (later in Activity_Create) - but I am still seeing a lot of crashes, so I don't think this is working. Or is it that Starter is taking longer to start than it takes for my code to get to the line that references the variable?

So - any ideas on how to make sure Starter is alive when Activity_Create is called? This is a hard one to test because I'm not sure if there's a way to force Android to stop the Starter service.

- Colin.
 

Multiverse app

Active Member
Licensed User
Longtime User
Looking at the crash reports I narrowed down the cause of the crash to a reference to a variable declared in the Starter service
Can you post the logs/crash report?
If you need to use the 'variable' after the activity has finished, try making a new service to complete the background task.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Can you post the logs/crash report?
If you need to use the 'variable' after the activity has finished, try making a new service to complete the background task.
I'm not trying to access the variable after the activity has finished - I'm trying to access it in Activity_Create when the app goes from background to foreground. Because of the way Android 8.x deals with services, it's killing the Starter service when the app goes into the background. The variable I'm trying to access is in Starter, I get a null pointer exception.

I'm trying to find a way to ensure that Starter is alive when the user brings the app back into the foreground but putting a call to StartService(Starter) doesn't seem to be working.


- Colin.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. See Services life cycle in v8+ section: https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/#content

2. Process global variables should still be valid after the service was destroyed. Can you post the Java generated code that causes the crash?

3. See the notes section in the updated Bluetooth tutorial: https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content
There is a suggested workaround there.

4. As written in the above notes, the next version of B4A makes some changes in the starter service to avoid this issue and cause the starter service to never be destroyed (until the process is killed).
I've already made that change. If you like I can send you a very early beta version with that change so you can try it.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks Erel - yes I had read that thread & that was where I got the idea to put the StartService call in Activity_Create. On reading it again, I see that the suggestion is to put it in Activity_Resume...

2. Process global variables should still be valid after the service was destroyed. Can you post the Java generated code that causes the crash?
Here's the error:


& here's the line of code from the generated java code:

Looking at that now, should I be using CallSubDelayed for that call in case Starter hasn't started yet?

3. See the notes section in the updated Bluetooth tutorial: https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/#content
There is a suggested workaround there.
Ahhh - OK, I had read the updated tutorial, but I missed the part about calling ExitApplication in the Service_Destroy of Starter.


Yeah - that would be great. If it works as expected I'd rather use that than put in a workaround now, then have to change it later. This particular app has really taken off in the past month & I'm trying to keep potentially breaking updates to a minimum.

- Colin.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Looking at that now, should I be using CallSubDelayed for that call in case Starter hasn't started yet?
Good. As I thought you are not accessing a global variable but rather using CallSub to call a sub in the starter service. Process global variables are not tied to the service life cycle and should always be available.

Keep the CallSub call and switch to the beta version.

Please send an email to support@basic4ppc.com.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User

Yes - my mistake. There is a CallSub & a reference to a variable declared in Starter.cOpts (my options class). I assumed the issue was the variable...

I have emailed you.

Thanks- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
starting service at Activity_Resume and calling ExitApplication at Service_Destroy did the trick for me.

I would have thought calling ExitApplication would negate the need to start the service manually. Shouldn't it restart when the app starts?

- Colin.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would have thought calling ExitApplication would negate the need to start the service manually. Shouldn't it restart when the app starts?
That's true. With the addition of ExitApplication in Service_Destroy you don't need to do anything else. The app will behave similar to the way it behaved in previous versions of Android, though it will be killed quicker when it is in the background.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@Computersmith64 I will send you a link to the "unpublished" version.

The starter service is now using the application context instead of the service context. This fact shouldn't matter. Report if you see any behavioral change.

Move your app to the background and monitor the logs. About a minute later you will see a message similar to: ** Service starter destroyed (ignored) **
Return to your app and the starter service should still be running.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks Erel - I will give it a try tomorrow. (It's almost bed time here in NZ now!)

- Colin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…