Android Question [B4X] [B4XPages] Splash Screen - hide or show

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
within my B4X App I'm using "[B4X] [B4XPages] Splash Screen" by @Erel (https://www.b4x.com/android/forum/threads/b4x-b4xpages-splash-screen.120851/)

I'm using it to take time while in the background I download the data from the server (using HttpJob .download(url)) which then go to the app cache.
In this way, the user waits only x seconds (fixed time that I set for the app splash) for the app to start and then during use the data display is practically instantaneous (because the data is already in the cache).

The code within my App Main (B4A) is:

B4X:
Sub Activity_Create(FirstTime As Boolean)  
    'B4XPages Splash
    Activity.LoadLayout("Splash")
    imgLogo.SetBitmap(xui.LoadBitmapResize(File.DirAssets, "logo.png", imgLogo.Width, imgLogo.Height, True))
    Sleep(6000) '---> I need about 6 seconds in order to donwload data from server
    StartActivity(actB4XSplash)
    Activity.Finish
End Sub

HOW IT WORKS NOW
The splash screen is displayed always: on the first app run and when the app is restored (already open) from the android task manager.
In this case (app is already open) the data is already within the app cache and I don't need to display the 6 seconds spash screen and force the user to wait when the data is already available (.download runs on the app start).

WHAT I NEED
I need that the 6 seconds splash screen runs ONLY when the app start for the first time (when I need to download the data from server).
Which is the best way to implement this mod ?

1) Checking the "FirstTime" boolean flag ?
2) Or checking if the app cache (maps) has been populated (n. items >0) ?

Thanks in advance for your help :)
Luca.
 

luke2012

Well-Known Member
Licensed User
Longtime User
It think that you can do it in a simple way (without writing anything in the Activity Main - avoid this):
https://www.b4x.com/android/forum/threads/solved-b4x-mainpage-firsttime.124457/post-777152

Hello @LucaMs, If I understood well (reading this post: https://www.b4x.com/android/forum/threads/solved-b4x-mainpage-firsttime.124457/)
the final solution (suggested by @Erel) is:
"Put the code in B4XPage_Created of B4XMainPage class. It runs once. Exactly like Activity_Create (FirstTime = True)".
Is it right ?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User

My current "B4XPage_Created" (without splash mod):

B4X:
Private Sub B4XPage_Created (Root1 As B4XView) 
    Root = Root1 
    Wait For (ShowSplashScreen) Complete (Unused As Boolean)
    Root.LoadLayout("home")
    B4XPages.SetTitle(Me, "Home - " & Application.LabelName)
    toast.Initialize(Root)
    DialogReg.Initialize (Root)
    '.....
End Sub

The new B4XPage_Created should be like this ?

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1 
    Wait For (ShowSplashScreen) Complete (Unused As Boolean)
    Root.LoadLayout("home")
    B4XPages.SetTitle(Me, "Home - " & Application.LabelName)
    toast.Initialize(Root)
    DialogReg.Initialize (Root)

   'B4XPages Splash
    Activity.LoadLayout("Splash")
    imgLogo.SetBitmap(xui.LoadBitmapResize(File.DirAssets, "logo.png", imgLogo.Width, imgLogo.Height, True))
    Sleep(6000) '---> I need about 6 seconds in order to donwload data from server
    StartActivity(actB4XSplash)
    Activity.Finish
    '.....
End Sub

The "B4XPages Splash" code has been moved from "Main / Activity_Create" to "B4XPage_Created" event.
Is it correct?
 
Last edited:
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Does it Work as required?
B4X:
If Yes Then Correct Else Wrong

You're right :) I have to try it out, but before I do I would like to try to avoid code errors before I even run it (since I'm not very familiar with this specific splash screen implementation).

SOLUTION
B4X:
Sub Activity_Create(FirstTime As Boolean)    
    'B4XPages Splash
    If FirstTime = True Then
        Activity.LoadLayout("Splash")
        imgLogo.SetBitmap(xui.LoadBitmapResize(File.DirAssets, "logo.png", imgLogo.Width, imgLogo.Height, True))
        Sleep(6000)
        StartActivity(actB4XSplash)
        Activity.Finish
    End If
End Sub

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)    
    Root = Root1
    Wait For (ShowSplashScreen) Complete (Unused As Boolean)     'B4XPages Splash
    Root.LoadLayout("home")
....
End Sub

In this way the splash screen is displayed only when the app starts and not when it's called from the Android task manager.
The "problem" is that this isn't a [B4X] [B4XPages] compatible code because "FirstTime" doesn't exit within B4i.
 
Last edited:
Upvote 0
Top