Other [quiz] Find the bug

Erel

B4X founder
Staff member
Licensed User
Longtime User
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
Just a thought, is "Image" a reserved word as it is in green?
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
So, well, I will exclude the 2 intializes from the FirstTime:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
   End If
     Image = LoadBitmap(File.DirAssets, "Image.jpg")
     SQL1.Initialize(File.DirInternal, "1.db", True)
End Sub
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Now I have a second activity which will load the image in an imageview. It still working like a charm....:)...no crash....

so the error is not as obvious like this one is
lQ5W96t.gif
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
My guess is that because the image is loaded only on the FirstTime there could be an occasion where the Main Activity is destroyed along with the bitmap resulting in the image having a Null value? But I'm not sure this stacks up becuase it is defined as a process Global and so should not get destroyed when still being used by other Activities???
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So, well, I will exclude the 2 intializes from the FirstTime:
It is a mistake to initialize SQL when FirstTime is not True. It will cause the SQL connection to be initialized multiple times (for example when the user changes orientation).

My guess is that because the image is loaded only on the FirstTime there could be an occasion where the Main Activity is destroyed along with the bitmap resulting in the image having a Null value? But I'm not sure this stacks up becuase it is defined as a process Global and so should not get destroyed when still being used by other Activities???
Almost, but not there yet...
 
Upvote 0

Jaames

Active Member
Licensed User
Longtime User
If you want to reload bitmap with other file, from another activity, it will not be possible
B4X:
 Main.Image = LoadBitmap(File.DirAssets,"Image2.jpg")
Not true , sorry

If you once change the image, and later exit the app, next time you start the app, image won't be default one
 
Last edited:
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Public Image As Bitmap 'public image
   Private SQL1 As SQL 'private sql object
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     Image = LoadBitmap(File.DirAssets, "Image.jpg")
     SQL1.Initialize(File.DirInternal, "1.db", True)
   End If
End Sub
The bug is : this App will do just nothing and show nothing, except initializing a db without any structure and loading an image. ;)
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
"RandomCoder said:
My guess is that because the image is loaded only on the FirstTime there could be an occasion where the Main Activity is destroyed along with the bitmap resulting in the image having a Null value? But I'm not sure this stacks up becuase it is defined as a process Global and so should not get destroyed when still being used by other Activities???"


Almost, but not there yet...
Ok, so is it possible that if not using the image on another Activity and the Main Activity gets destroyed then there is a time when we no longer have the Image declared. Attempting to use the Image or load a Bitmap from another Activity will result in an error because Main no longer exists and so the reference to Main.Image throws an error?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Solution

The problem is with the image:
B4X:
Sub Process_Globals
   Public Image As Bitmap 'public image
   Private SQL1 As SQL 'private sql object
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     Image = LoadBitmap(File.DirAssets, "Image.jpg")
     SQL1.Initialize(File.DirInternal, "1.db", True)
   End If
End Sub

The reason that this image is public is that we plan to use it from other activities.

For example:
B4X:
'Activity2
Sub Activity_Create(FirstTime As Boolean)
 Activity.SetBackgroundImage (Main.Image)
End Sub

During development everything will look fine. The application will always start from the main activity and the image will live as long as the process lives.

However it will eventually fail when it is installed on the user device.
The user will press on the home key when the second activity is visible. The process will move to the background.
At some point the OS will kill the process as it is in the background.
Later when the user returns to the app the process will start from the second activity.
Main activity was not created yet so its Activity_Create wasn't executed. This means that the image is not initialized at that point.

You can see it during development with this developer setting:

upload_2015-7-14_8-48-0.png


Run your app in Release mode, switch to the second activity, press on home and start a different app. Return to your app and in most cases it will start from the second activity directly.

To solve this issue you can add a code module that holds all the public resources:
B4X:
Sub Process_Globals
   Public Image As Bitmap
   Private alreadyInitialized As Boolean
End Sub


Public Sub Init
   If alreadyInitialized = False Then
     Image = LoadBitmap(File.DirAssets, "Image.png")
    'all other resources as well
     alreadyInitialized = True
   End If
End Sub

Now you should call this Init sub from Activity_Create (and Service_Create) of all activities and services.

Related quiz: https://www.b4x.com/android/forum/threads/37899/#content
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Suppouse all the variables I need became from procesess that can't be moved to the Code module (add-billing, etc)
It is ok to use the Code module just to keep the variables in the Sub Process_Globals
And set that variables from other modules with

MyCodeModule.Myvarible = xxx

Does this variables will keep its values without setting it again in Sub init? mhhh I think not.

And how if I use a service to hold the Process variables?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Suppouse all the variables I need became from procesess that can't be moved to the Code module (add-billing, etc)
It is ok to use the Code module just to keep the variables in the Sub Process_Globals
And set that variables from other modules with

MyCodeModule.Myvarible = xxx

Does this variables will keep its values without setting it again in Sub init? mhhh I think not.

And how if I use a service to hold the Process variables?

Is THIS your answer to the QUIZ? BTW: Erel already posted the answer. No need to guess the answer
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
You didn read my post or its difficult for me to make me undestand, bad luck.
New q:

How if I use a Service module to hold all the Process variables?
May be I have to use Service.StartForeground?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I did real you post. That why i asking what i ask

So you are Not answering the Quiz but asking offtopic questions?

You Know that this is the WRONG PLACE to ask questions?

Hint: Considered to create a New thread for yor question instead of asking at the WRONG place
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Sory I dont know that this is the WRONG PLACE to ask questions
Why is it wrong, Im asking about the Thread, why to start another?
 
Upvote 0
Top