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

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:
B4X:
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.
B4X:
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.
 

Attachments

Last edited:

LucaMs

Expert
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.
So, if this is possible, this means that it becomes deprecated the use of global variables (I don't love them so much, anyway :))
 

Cableguy

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

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!
 

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