Android Question avoiding "global" toasts on Chromebooks

Dave O

Well-Known Member
Licensed User
Longtime User
Short version:
On Chromebooks, when my app shows a toast message every 60 seconds (checking for data updates), the toast gets shown even when my app isn't the current app. Is there a way to have my app's toasts only appear when the app is active?

Longer version:
My app checks a server for updates every 60 seconds while the app is active (foreground) and shows the result as a toast. That's OK on Android, where only one app is active at a time.

However, some of my users run my app on Chromebooks too, and apparently they're seeing that periodic toast even when my app is open but not active. And they find that distracting.

I'm wondering if there's a smarter way to handle this. I would like to continue supporting Chromebook use.

(I may redesign the frequency and logic of these toasts, but really for now I'm looking for a way to suppress toasts when my app is not active.)

Any help appreciated!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Check if your app goes to background.

Thanks. What's the current best way to check for an app being in the background?

I don't think my app is paused when inactive on the Chromebook. The fact that the timer is still running in my Main activity suggests that a simple check like isPaused is not correct in this case?

(Complicating matters is that I don't have a Chromebook to test on.)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Thanks. What's the current best way to check for an app being in the background?

I don't think my app is paused when inactive on the Chromebook. The fact that the timer is still running in my Main activity suggests that a simple check like isPaused is not correct in this case?

(Complicating matters is that I don't have a Chromebook to test on.)
You can check if it is in the background by pause event
1.b4a Activity_Pause
2. b4x B4XPage_Appear
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
You can check if it is in the background by pause event

I disable the check-for-updates timer in my Pause event, but it still fires when the app is inactive on a Chromebook, so the app is apparently not paused, yet still not the current app.

(My app is a default Android project, not B4XPages.)

Is there a way to determine the current app? If so, I could compare it to my app (via package name?) and determine that my app is not current (and disable the toast).
 
Upvote 0

teddybear

Well-Known Member
Licensed User
No need to disable timer
You need to use a global variable to check if it is in the background. such isPause
In Activity_Pause set isPause to true
In Activity_Resume set it to false.
Check it before toast
 
Last edited:
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
For old-school B4A apps (not using B4XPages), it's good practice to pause timers in the onPause event (according to the docs).

I know that the app is NOT paused because the timer is still firing and calling my check-for-updates code (which in turn shows the toast).
 
Upvote 0

teddybear

Well-Known Member
Licensed User
For old-school B4A apps (not using B4XPages), it's good practice to pause timers in the onPause event (according to the docs).

I know that the app is NOT paused because the timer is still firing and calling my check-for-updates code (which in turn shows the toast).
As DonManfred said at post#2
Check if your app goes to background and don´t show them if so.
it should be like this
B4X:
if isPause = false then

   toast(...)

end if
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
That's true on Android, where the OnPause event fires just before the app goes to the background.

What I'm saying is that Chromebooks apparently don't work that way. It seems that the app going to the background is NOT paused, so your code above would never set isPause to TRUE.

Hence my question about how to find out which app is current (or if my app is backgrounded but not paused).
 
Upvote 0

teddybear

Well-Known Member
Licensed User
No need to disable timer
You need to use a global variable to check if it is in the background. such isPause
In Activity_Pause set isPause to true
In Activity_Resume set it to false.
Check it before toast
Did you try this? does not it work when the app going to the background?
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Log("isPause")
End Sub
 
Upvote 0
Top