Android Question Unable to bring a B4XPage app to foreground

toby

Well-Known Member
Licensed User
Longtime User
When my app is in the background, I use a timer to play a sound and bring the B4XMainPage to the foreground when certain conditions are met. The sound was played and B4XPage.ShowPage("MainPage") was called, but the page didn't appear and no log entry either.

I tried put the timer first in B4XMainPage and then in Starter service, and the result is the same.

Did I miss anything, or it's as designed?

timer tick event:
Sub tmrNotification_Tick
    Log("entering tick")
    If Rnd(0,9)>5 Then
        'play sound file
        soundPool1.Play(soundPoolLoadId,  1, 1, 1 , 0, 1)
        Log("playing soundPool1")
        B4XPages.ShowPage("MainPage")  'no page nor log appears
    End If
End Sub
 

Attachments

  • backgroundNotification.zip
    99.2 KB · Views: 72

Mahares

Expert
Licensed User
Longtime User
the page didn't appear and no log entry either.
I got rid of all the code you have in the Starter service and changed B4XMainPage. I hope this is what you are looking for. I see the customlistview adding items after it beeps
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim tmrNotification As Timer
    'play notification sound
    Dim soundPool1 As SoundPool 'lib: audio
    Dim soundPoolLoadId As Int=0
    Private CustomListView1 As CustomListView
    Dim csb As CSBuilder
    Dim Dialog As B4XDialog
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
    tmrNotification.Initialize("tmrNotification",10000)
    soundPool1.Initialize(2)
    soundPoolLoadId=soundPool1.Load(File.DirAssets, "notificationsound.wav")
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dialog.Initialize(Root)
    tmrNotification.Enabled=True
    Dim rs As ResumableSub =getDrawOverOtherAppsPermission
    Wait For (rs) complete(intResult As Int)
   
    displayxCLV
   
End Sub

Sub displayxCLV
    CustomListView1.AddTextItem($"app starts ${DateTime.Time(DateTime.Now)}"$, DateTime.Time(DateTime.Now))
End Sub

Sub tmrNotification_Tick
    Log("entering timer_tick")
    If Rnd(0,9)>5 Then
        'play sound file
        Log("playing sound")
        soundPool1.Play(soundPoolLoadId,  1, 1, 1 , 0, 1)
        displayxCLV
    End If
End Sub
Private Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    Log(Value)
End Sub
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
My bad for the confusion. What I meant was that nothing happened, the page didn't appear and no error was logged, after the execution of the following line of code
B4X:
  B4XPages.ShowPage("MainPage")

I tried out your code and the result is the same.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Sorry ,it didn't work for me.
This was how I tested: After the app appeared, tapped Home key to put the app in the background. When I heard the sound, the page didn't re-appear.

Tested with Google Pixel 4A, Android 11, B4A 11.0. I don't think latest version of B4A would make any difference.

Thank you very much for trying to help🙂
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
By the way, it works with default project type (Activities) by calling
B4X:
StartActivity(Main)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
By the way, it works with default project type (Activities) by calling
B4X:
StartActivity(Main)
One of the great advantages, probably the greatest advantage, of B4XPages is that the Activity is never paused.
For this reason StartActivity does not work, the Activity is already active.

It is necessary to see how it happens under the hood,how the Activity is prevented from pausing, studying the source of B4XPages.xlib or, since now I have a big headache, wait for Erel.
It is likely that you will have to kill the Activity with the Finish method and then use StartActivity.


BTW, it doesn't work very well for me with "classic B4A": I set a timer that raises every 2 seconds, in the event I check if Main is paused and if it is I call StartActivity; well, only every 3 events the Main is brought to the foreground.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
One of the great advantages, probably the greatest advantage, of B4XPages is that the Activity is never paused.
For this reason StartActivity does not work, the Activity is already active.

It is necessary to see how it happens under the hood,how the Activity is prevented from pausing, studying the source of B4XPages.xlib or, since now I have a big headache, wait for Erel.
It is likely that you will have to kill the Activity with the Finish method and then use StartActivity.


BTW, it doesn't work very well for me with "classic B4A": I set a timer that raises every 2 seconds, in the event I check if Main is paused and if it is I call StartActivity; well, only every 3 events the Main is brought to the foreground.
I am attaching a project that seems to work. I haven't run many tests 🤕
 

Attachments

  • lm.zip
    13.7 KB · Views: 89
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
I'm aware of the benefits of b4xpage project type over traditional one, that's why I'm switching to it to take advantage of it, learning how to make this feature in question work.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
It is becoming more and more complicated to comply with Android changes. Every time there is a new version, and customers acquire new devices, calls for problems are continuous and you have to rack your brains to figure out how to solve the situation.
And that is without uploading the applications to "google play", in that case it must be impossible to comply with so many requirements.
 
Upvote 0

Sandman

Expert
Licensed User
It is becoming more and more complicated to comply with Android changes.
I'm dealing with that by trying to do nothing to "clever". Just middle-of-the-road functionality. I focus on making the app itself as good as possible.

Starting from Android 10, it is more difficult to show an activity while the app is in the background:
In all fairness, this must be considered a good thing for at least 99% of the users. I would consider it an unwanted experience to have an app force itself to the foreground. "If you want to come to the foreground, you tell me with a notification!" (My internal voice talking to app.)

That said, I have no problem seeing that there are some edge cases for specialized devices (where the app isn't in the store) where this might be the desired thing. A heart monitor device in a hospital comes to mind. (If such a device exists, running Android.)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
That said, I have no problem seeing that there are some edge cases for specialized devices (where the app isn't in the store) where this might be the desired thing. A heart monitor device in a hospital comes to mind. (If such a device exists, running Android.)
It is possible to show activities from the background. B4A-Bridge does it (see the above link).
 
Upvote 0

Sandman

Expert
Licensed User
It is possible to show activities from the background. B4A-Bridge does it (see the above link).
Yeah, I know. And that would be a very good example of an edge case. But as for "ordinary" apps, I would consider it a bad thing. Imagine the podcast app popping into foreground with a message "Downloaded seven new episodes". It would drive me crazy to get that, especially when I was typing on my mobile. Like I am just now. 🙂
 
Upvote 0
Top