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 Active 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:
    Code:
    Dim bmp1 As Bitmap
    bmp1.Initialize(
    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.
    Code:
    java.io.FileNotFoundException:
        at android.content.res.AssetManager.openAsset(Native Method)
        at android.content.res.AssetManager.open(AssetManager.java:
    409)
        at android.content.res.AssetManager.open(AssetManager.java:
    383)
        at anywheresoftware.b4a.objects.streams.File.OpenInput(
    File.java:202)
        at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:
    498)
        at b4a.example.applostfocus.activity2._activity_create(activity2.java:
    329)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    187)
        at b4a.example.applostfocus.activity2.afterFirstLayout(activity2.java:
    100)
        at b4a.example.applostfocus.activity2.access$
    100(activity2.java:17)
        at b4a.example.applostfocus.activity2$WaitForLayout.run(activity2.java:
    78)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    136)
        at android.app.ActivityThread.main(ActivityThread.java:
    5214)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    814)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    630)
        at dalvik.system.NativeStart.main(Native Method)
    java.io.FileNotFoundException:
    ** 
    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 Active 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 Active 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.
     
Loading...