Setting Bitmaps to Null

Bill Norris

Active Member
Licensed User
Longtime User
Have several imageviews with bitmaps on an activity and I am setting the bitmap to null to conserve memory when leaving the activity. I have a "next"
coded as follows:

B4X:
sub btn_next_Click
startactivity("activity_2")
imageview1.bitmap=null
imageview2.bitmap=null
activity.finish

the code runs fine, but what happens is a slight (undesirable) pause as the imageviews go blank before activity_2 loads. I tried putting Activity.finish before the bitmap=null lines and the same behavior occurs. Also tried moving those lines to activity_pause -- same thing occurs. How can I get the usual "instantaneous" transition to the next activity and unload those bitmaps in the background, rather than have the user see a blank screen, even though it is a short time.
 

JonPM

Well-Known Member
Licensed User
Longtime User
It really isn't necessary in Android to set bitmaps to null. The garbage collector will free that memory when the bitmaps have no more references to them.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
How exactly does the internal message queue, I guess it would be called, in B4A work? I've got some activities with things like this I need to do as well and wondered the best/safest way to call it. When you call startactivity your original activity that called it should be paused, but that appears to be a queued message or does it take place right when the sub finishes? Same thing with Activity.Finish? It is supposed to be queued too right? So, couldn't he just do:

B4X:
sub btn_next_Click
   Activity.Finish
   StartActivity("activity_2")
end sub

Ending the activity would remove the views and no references to the bitmaps, so no need to set to null. In my tests here I made a test sub executing a toastmessage and the sub still finishes(Shows the message) and the Activity immediately closes.

Or, perhaps to enhance the flow he could set them to null before so it isn't trying to do it while loading the activity.

B4X:
sub btn_next_Click
   imageview1.bitmap=null
   imageview2.bitmap=null
   StartActivity("activity_2")
   Activity.Finish
end sub

Seems unnecessary to set to null though since Finishing the Activity does the same thing. And the delay is most likely always going to be there since no matter how it is done the end result of one activity closing and another opening is occurring.

I haven't tested it, but what happens behind the scenes if a DoEvents is stuck in there? Like after StartActivity would it pause our Activity and not allow the sub to finish. Or if after Activity.Finish in my first example would it not Start the next Activity (This one I did test and my ToastMessage still shows...so what does DoEvents accomplish in that case? Or, is there code in there to prevent stuff like this or deadlocks?) Is it safe to finish first like this or is there cases where it may kill the activity before the sub finishes?

At what point is Activity_Pause fired too? Is it queued, is it right when StartActivity is called? Coming from Windows programming I wonder if events here are the same. Like if I change text of an edit box in its TextChanged event I usually add code to prevent endless looping. Does B4A fire the event right when I change that text like Windows? Is it queued? Does it call it like a Recursive Function?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…