Android Question How to Free/Release components

DeviousD

Member
Licensed User
Longtime User
Morning All,

Rather a simplistic question i suppose, but have searched the beginners guide and maybe my search terminology is not correct.

When creating for example a label dynamically assigning values, including the tag etc how do I go about to free that component, and how to i "step" through all label components and then release/free up specific ones from the current activity? Obviously i have set the tag, but how does one step thru and release for example label99 ??

If i'm creating labels every second, this will eventually start to take up to much memory and crash that app?

i understand that if i call the invalidate command, it will re-draw the activity but its not what im wanting to do...
 

eurojam

Well-Known Member
Licensed User
Longtime User
Hi Devious,
you can work with an array of labels for example. Let's say the maximum of Labels you'll need will be 100. Then you can define an Array of labels or a list or map of labels. If you don't need them anymore, you can reuse them. The amount of labels will then not be more then your maximum...it is a construct like this:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim labelMap As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    labelMap.Initialize
    For i = 0 To 100
      Dim l As Label
      labelMap.Initialize("label")
      labelMap.Put(l,l) 'the label is object and key
    Next 
End Sub

I hope, this points in the right direction....

stefan
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
It is my understanding that Objects will automatically be removed by the system so long as they are no longer used. To this end I think that you would need to remove the Label from the active view and ensure that you no longer reference it.
Assuming that you're Labels are on an Activity you could itterate through all the labels and use Activity.RemoveViewAt(Index) to remove the required Label. https://www.b4x.com/android/help/views.html#activity_removeviewat this is also available for Panels.
The system will the dispose of it as required.

If you are removing a lot of Labels it might be quicker and easier to remove all the views and then load back the ones you want to keep?
 
Upvote 0
Top