Android Question B4A 3.2, the IDE and the FirstTime Variable

air cover

Member
Licensed User
Longtime User
See if you are clever enough to spot the problem!

I am using B4A version 3.2 with MySQL, and something bizarre is transpiring.

Clicking the app icon on the phone, the app only runs flawless "every other time."

However, clicking the Play button on the IDE makes the app work every time (running from the B4A 3.2 IDE).


On the "every other time" that it fails when clicking the phone icon, it triggers an error in the app for a List object not being initialized, "Continue?" Say No. App shuts down hard. Click the phone icon to run the app again. Works fully.

*saying Yes to that error message and then exiting app "normally" means that it still won't work the next time that you run it...must say "No" to the Continue question for the app to have db access the next time the app is run. Saying "No" triggers a hard stop of the app by the Android OS. Then click the app icon again, it runs flawlessly, etc.

Works every other time.







**the actual Android error message is:
"An error occurred in sub: java.lang.RuntimeException: Object should first be initialized (List).
Continue?
 
Last edited:

air cover

Member
Licensed User
Longtime User
OK, I inserted Log("a,b,c...") lines into Activity_Create, and here is what I'm seeing:

Everytime that I press the play button to start my app from the B4A IDE, statements inside the "FirstTime = True" are getting properly executed.

However, when I start the app by pressing the phone icon on its screen, the "if FirstTime = True" statements are only getting properly executed every other time...or a better way to say it may be only after a hard stop from the Android OS.

So when my app is run by pressing its phone screen icon button, it will run correctly that one time, but it won't run correctly on subsequent times until after the Android OS shuts the app down hard. If the user just exits the app by hitting the backspace key, then the app won't process the "if FirstTime = True" statements the next time that the app icon is pressed on the phone's screen.


This is probably embarassing. I suspect that I'm missing some newbie fine point on how to insure that the first time the app is run from the icon, the "if FirstTime = True" statements are properly executed.


Right now, with my lack of understanding, my app is behaving as though the user is merely pausing the app instead of terminating the app, and when the user presses the app's icon a subsequent time, my app is behaving as though it is being resumed instead of started anew.
 
Upvote 0

air cover

Member
Licensed User
Longtime User
I solved the problem for my app. The issue is that the Android OS isn't reliably resetting the FirstTime variable.

No big deal, I created a synthetic First Time global variable to deprecate the B4A FirstTime variable.


B4X:
Sub Process_Globals
Dim SynFirstTime=True As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Ignore FirstTime, use SynFirstTime instead

If SynFirstTime = True Then
      'do stuff
end if
'do more stuff
SynFirstTime=False
End Sub

Sub Activity_Pause (UserClosed As Boolean)
SynFirstTime=UserClosed
End Sub

Sub Exitapp_click
    SynFirstTime=True
    Activity.Finish
End Sub

You will not see the Android FirstTime problem when testing your app from the B4A IDE because the IDE is resetting the FirstTime variable correctly.

However, you *will* see the Android OS causing havoc with the proper setting of the FirstTime variable when you run your app by hitting its icon on a smartphone...unless you use something similar to my solution above by creating and setting a Synthetic FirstTime (SynFirstTime) boolean for your activity_create sub.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

air cover

Member
Licensed User
Longtime User

Thanks. That makes sense. And since users are unpredictable, my Synthetic First Time solution above will work either way for all of us, whether the process was killed or re-used.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You should work within the android framework the way you describe the first time variable working within activity create is correct.

Without knowing what you are putting within your synfirsttime section, It is not possible to say what problems it will create. But it will create some down the line.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…