Other [quiz] Find the bug

Discussion in 'Android Questions' started by Erel, Jul 13, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    No.
     
  2. sorex

    sorex Expert Licensed User

    I never work with multiple activities and I don't have a clue where to look so I give up :)
     
    Peter Simpson likes this.
  3. mark35at

    mark35at Well-Known Member Licensed User

    Just a thought, is "Image" a reserved word as it is in green?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    No. It is green because the forum parser treats it as B4J Image type.
     
  5. imbault

    imbault Well-Known Member Licensed User

    So, well, I will exclude the 2 intializes from the FirstTime:
    Code:
    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
     
  6. eurojam

    eurojam Well-Known Member Licensed 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
    [​IMG]
     
    LucaMs likes this.
  7. RandomCoder

    RandomCoder Well-Known Member Licensed 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???
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    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).

    Almost, but not there yet...
     
  9. Jaames

    Jaames Active Member Licensed User

    If you want to reload bitmap with other file, from another activity, it will not be possible
    Code:
    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: Jul 13, 2015
  10. DonManfred

    DonManfred Expert Licensed User

    It must be image.jpg! Files in File.DirAssets must be lowercase
     
  11. Jaames

    Jaames Active Member Licensed User

    They can , you can use name you want, b4a will internally lowercase them.
     
    DonManfred likes this.
  12. DonManfred

    DonManfred Expert Licensed User

    Dammit :D
     
    Jaames likes this.
  13. imbault

    imbault Well-Known Member Licensed User

    The bug is : this App will do just nothing and show nothing, except initializing a db without any structure and loading an image. ;)
     
  14. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Solution

    The problem is with the image:
    Code:
    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:
    Code:
    '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:
    Code:
    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: http://www.b4x.com/android/forum/threads/37899/#content
     
  16. Alberto Michelis

    Alberto Michelis Active Member Licensed 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: Jul 29, 2015
  17. DonManfred

    DonManfred Expert Licensed User

    Is THIS your answer to the QUIZ? BTW: Erel already posted the answer. No need to guess the answer
     
  18. Alberto Michelis

    Alberto Michelis Active Member Licensed 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?
     
  19. DonManfred

    DonManfred Expert Licensed 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
     
  20. Alberto Michelis

    Alberto Michelis Active Member Licensed 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?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice