Android Question SplashScreen not splashing

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

My primary problem is that my App is slow to launch [1.5 - 2 Secs]. I have assumed that this is due to a lot of work in the Designer Script associated with Activity.LoadLayout("Portrait"). [Over 250 lines]
I have attempted to give the user the impression that the App has started by firing up a Splash Screen Activity.LoadLayout("Splash")

To create the Splash Screen I have followed the:
Android Tutorial How to make a simple splash scree (that you can understand) Tutorial Link

The code below is intended to load/display the Splash Screen while the Script is working on the other layout.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Display Splash layout
    Activity.LoadLayout("Splash")
    SplashPanel.BringToFront
    SplashPanel.Visible=True
    plashTimer.Initialize("SplashTimer", 2000)    'splash screen - title screen - 2 secs
    SplashTimer.Enabled=True
   
    Activity.LoadLayout("Portrait")
    Canvas1.Initialize(Activity)
    MP.Initialize2("MP")
    other stuff etc.

The problem is that there is no sign of the Splash Screen.

First question.
Is my initial assumption that the slow start is due to the time taken to run the script file for "Portrait" layout valid?

Second question.
Why don't I see the Splash Screen?


Thanks in advance.

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Siam thanks for the reply but to give you more detail.

I had already implemented the SplashTimer_tick sub as shown in point 3 of the tutorial. This doesn't show the second LoadLayout occurring here.
I can see the logic that starting a second LoadLayout will immediately overwrite the first layout but if this is the case, delaying the second LoadLayout only delays my original problem.

The original problem is that when the user clicks on the icon to launch the application they are left looking at the icon for 1.5 to 2 secs before the "Portrait" layout is displayed. Some people are very impatient.

With the Splash Screen implementation my understanding was that the Splash Screen would be displayed while the Portait Layout was being manipulated by the Script file.
If the Activity.LoadLayout("Portrait") immediately overwrites the Splash Screen, there is no point to the Splash Screen.

If the Splash Screen is not the answer, I am open to suggestions as to how give the user the impression that things are happening as soon as the icon is clicked.

Regards Roger
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I believe the problem you have is that you are not allowing the GUI to update which is the purpose of the Splash timer. The Android OS will only refresh the display at the end of the Sub or if you use DoEvents it will try to refresh the screen mid subroutine. So ideally you need to move the second LoadLayout into the Splash tick event, but it doesn't need to wait 2s before doing this. You could set a 40ms delay. It needs to be just enough time to allow the Splash to load. You might get away with a 10ms delay?
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi RandomCoder,

Unfortunately moving the Activity.LoadLayout("Portrait") appears to leave the main process cycle to attempt to continue with the code execution and of course creates an error.

B4X:
Sub  SplashTimer_tick
    SplashCounter = SplashCounter + 1                     'SplashCounter is 20ms
    If SplashCounter = 2 Then  
        Activity.LoadLayout("Portrait")                      '40ms delayed
    End If
    If SplashCounter = 100 Then
        SplashPanel.Visible=False
        SplashTimer.Enabled = False
    End If
End Sub



B4X:
Sub Activity_Create(FirstTime As Boolean)
    SplashCounter = 0
    'Display layout
    Activity.LoadLayout("Splash")
    SplashPanel.BringToFront
    SplashPanel.Visible=True
    SplashTimer.Initialize("SplashTimer", 20)    'splash screen - title screen - 20 msecs
    SplashTimer.Enabled=True    
   
    'Activity.LoadLayout("Portrait")             Code execution continues without Portrait layout loaded
    Canvas1.Initialize(Activity)
    MP.Initialize2("MP")
    MP.Load(File.DirAssets,"Windows XP start.wav")
    FileLists.Initialize
    FileListing1.Initialize
    FileListing2.Initialize

    MyFont2 = Typeface.LoadFromAssets("Timeless.ttf")
    lblCON.Text  = ""                                ' lblCON is in Portrait layout.  Initialization error 
    Rdisplay.Typeface = MyFont2
    Idisplay.Typeface = MyFont2

Perhaps I am still missing something.

Regards Roger
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is my initial assumption that the slow start is due to the time taken to run the script file for "Portrait" layout valid?
You need to measure it. Run it with the script commented out to test it.

My guess is that it is not related to the designer script. Are you creating many views? You can consider splitting the layout file into several smaller files and only load the views that you need when the program starts.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel/RandomCoder,

Erel, it seems that the Script is NOT the [main] source of the problem, thanks for the hint of commenting out the script. That answers first question in the original post.
The solution was provided by RandomCoder but I missed it while looking at SplashScreens, Timers etc.
"Doevents" immediately after Activity.LoadLayout("Portrait") eliminates most of the delay.

I can understand why I don't see the splashscreen [Second question], but I would be interested if there is a sample code with a working splashscreen.


Regards Roger
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hi RandomCoder,

Unfortunately moving the Activity.LoadLayout("Portrait") appears to leave the main process cycle to attempt to continue with the code execution and of course creates an error.

First of all sorry for the delayed reply.
You do not need to count tick events as in your sample code. All that is required is to load the Splash screen layout and start your timer in Activity _Create. Then in the tick event you can load the main layout and stop the timer as it has now served its purpose the Splash will remain displayed until the main layout overwrites it. But you will need to move all code associated with the Main layout from Activity_Create and into the tick event to prevent the error you are getting because, like you said, the execution of the sub continues to the end of the sub after loading the Splash layout. Hope that makes sense!
 
Upvote 0
Top