Android Question B4XPages. How to show Activity if it was closed?

aberezhnykh

Member
Licensed User
Longtime User
My application (player) uses the B4XPages library. The application is designed to work in the background. Everything is fine as long as the Activity is not closed by the system. However, the B4XMainPage class is initialized (music is playing). Is there a way to display the Activity without reinitializing the B4XMainPage in order to access the controls again and display the necessary information to the user?

Thank you!
 

aberezhnykh

Member
Licensed User
Longtime User
Thanks Erel for the quick response!

B4X:
Private Sub Activity_Create(FirstTime As Boolean)

    Dim pm As B4XPagesManager


    pm.Initialize(Activity)


'    If B4XPages.IsInitialized Then
'        B4XPages.MainPage.B4XPage_Created(Activity)
'        B4XPages.ShowPage("MainPage")
'    Else
'        pm.Initialize(Activity)
'    End If
End Sub

However, if the activity is not visible but the application continues to run, the user opens the application again. Again Activity_Create, again pm.Initialize (Activity). As a result, everything looks like the application has stopped. I started experimenting and checking if B4XPages is initialized. In this case, the behavior of the application is quite expected. But am I doing it right?
 
Upvote 0

aberezhnykh

Member
Licensed User
Longtime User
It ended up working for me. In B4XPage_Created, I again add the layout and customize the interface elements.

B4X:
Private Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    If B4XPages.IsInitialized Then
        B4XPages.MainPage.B4XPage_Created(Activity)
        B4XPages.ShowPage("MainPage")
    Else
        pm.Initialize(Activity)
    End If
End Sub

Thank you!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
However, if the activity is not visible but the application continues to run, the user opens the application again. Again Activity_Create, again pm.Initialize (Activity)
As a result, everything looks like the application has stopped.
If Activity_Create is executed this means that the app was not running, the process (your app) was killed by the O.S. - "has stopped", as you wrote.

Your code is not correct, mainly because you souldn't call B4XPage_Created (also, this event routine will and should/must receive a Panel, Root).

You should hardly ever touch the code in the Activity Main, when using the B4XPages, except in rare cases (and this is not one of them).

What you should do is use a service, put the sound part into it and try to make it survive (see: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/)
 
Upvote 0

aberezhnykh

Member
Licensed User
Longtime User
If Activity_Create is executed this means that the app was not running, the process (your app) was killed by the O.S. - "has stopped", as you wrote.

Your code is not correct, mainly because you souldn't call B4XPage_Created (also, this event routine will and should/must receive a Panel, Root).

You should hardly ever touch the code in the Activity Main, when using the B4XPages, except in rare cases (and this is not one of them).

What you should do is use a service, put the sound part into it and try to make it survive (see: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/)
Dear LucaMs, it is difficult for me to answer you somehow reasonably, since my experience is simply negligible in comparison with yours. All code, with the exception of some helper classes, is located inside MainPage. For me it seemed the most convenient for cross-platform implementation. However, when the system kills the Activity (I don't physically see it anywhere), then the process is not stopped. MainPage is initialized. The player works without a visible Activity. Indeed, if I click on the notification icon or on the application icon, then the Activity_Create event occurs. I don't know how to explain it.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
All code, with the exception of some helper classes, is located inside MainPage. For me it seemed the most convenient for cross-platform implementation.
It depends on the application; probably in your case a single page is sufficient. However, having multiple B4XPages does not affect cross-platform development at all.

However, when the system kills the Activity (I don't physically see it anywhere), then the process is not stopped. MainPage is initialized. The player works without a visible Activity
The fact that you don't see the Activity doesn't mean it doesn't exist, it could be in background (and at that moment it means your app is in background).
Whenever Activity_Create runs this means your app is starting from scratch.
 
Upvote 0

aberezhnykh

Member
Licensed User
Longtime User
Yes, I definitely agree with you! The app continues to live. Can I show Activity again? Here it is not clear to me how to do it, unfortunately.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes, I definitely agree with you! The app continues to live. Can I show Activity again? Here it is not clear to me how to do it, unfortunately.
You don't have to do anything; the user will use the device methods to return to your app (he will choose it from the list of recently used apps).

Do not write anything in the Activity Main; let alone Activity.Finish.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Can I show Activity again?
Sorry, I was watching a TV program.

With the B4XPages it is as if you never see the Activity Main; or rather it is always active. What you have to manage is the B4XMainPage and its events (also because you have only this B4XPage in your project).

I think it will be useful for you to know the events of the B4XPages:

11. You can see the list of events by writing: Sub + space + tab and choosing B4XPageManager. The event name is always B4XPage.
https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-743752
 
Last edited:
Upvote 0
Top