Android Question New activity lost variables in Process Global of the Main activity

Discussion in 'Android Questions' started by asales, Jun 9, 2015.

  1. asales

    asales Well-Known Member Licensed User

    I have an problem with this example (in attached).
    The example shows a image and a text in Activity2, using the information of the Process Global variables in the Main acitivity:
    Dim bmp1 As Bitmap
    File.DirAssets, Main.img1)     
    ImageView1.Bitmap = bmp1 
    Label1.Text = Main.text1
    If I change to another app and return to this example, sometimes, I get this error (the information of variable is lost). See the log and the sequence of images.
        at android.content.res.AssetManager.openAsset(Native Method)
        at anywheresoftware.b4a.objects.streams.File.OpenInput(
        at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(
        at b4a.example.applostfocus.activity2._activity_create(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at anywheresoftware.b4a.BA.raiseEvent2(
        at b4a.example.applostfocus.activity2.afterFirstLayout(
        at b4a.example.applostfocus.activity2.access$
        at b4a.example.applostfocus.activity2$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at dalvik.system.NativeStart.main(Native Method)
    Activity (activity2) Resume **
    screen1.png screen2.png screen3.png screen4.png

    The information of process global variable "Main.img1", with the name of image file "android.jpg" is empty and raises the error "File Not Found".

    I don't know what is happening.

    Thanks in advance for any tip to fix this problem.

    Attached Files:

    Last edited: Jun 9, 2015
  2. klaus

    klaus Expert Licensed User

    In the zip file you posted there is no android.jpg file !
    You should add this file in the Files Tab !
  3. asales

    asales Well-Known Member Licensed User

    Thanks Klaus. I updated the file.
  4. klaus

    klaus Expert Licensed User

    I can't reproduce it.
    I don't get this error.
  5. Erel

    Erel Administrator Staff Member Licensed User

    I guess that your device is set to kill background processes very quickly. When you return to your app only Activity2 is created so Main.img1 is empty.

    Note that if you set the value of img1 in process globals then it would have worked properly.
  6. LucaMs

    LucaMs Expert Licensed User

    So, if this is possible, this means that it becomes deprecated the use of global variables (I don't love them so much, anyway :))
  7. Cableguy

    Cableguy Expert Licensed User

    Yes and no. I sometimes use the same variable names, in sake of logic, in different activities/modules in the same project. Using only process globals, which are accessible across modules, would force (me) to use different variables names.
    Globals however are module/activity restricted.
  8. LucaMs

    LucaMs Expert Licensed User

    I think it's better to save data in files or DBs and use a lot routines with parameters.

    Especially after this news!
  9. asales

    asales Well-Known Member Licensed User

    Thank you all. I'll check the tips. Maybe save the data in db is the best option, because I will select the image in random mode.
  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