Android Question Remove and destroy custom view

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
I have a panel to which I add custom views. In addition, I keep a list to that I add the items for convenience. Now occasionally I need to reinitialize and remove everything.

Remove:
Sub devicesremove()
    For i = (devices_l.Size - 1) To 0 Step - 1
        devices_l.Set(i, Null)
        devices_l.RemoveAt(i)
    Next
    'devices_l.clear does not work either
    devices.Panel.RemoveAllViews
    devices.Panel.Height = 0
End Sub

Sub additem()
    Dim item As cv_device
    item.Initialize(something, something, something)
    item.add(devices.panel, 0dip, devices.Panel.Height, devices.Panel.width, 100dip)
    devices.Panel.Height = devices.Panel.Height + 100dip
    devices_l.Add(item)
End Sub

Inside of the custom view cv_device there is a timer that just keeps fireing and the cv is still there. The list just gets longer even after the deviceremove.

Any way I can destroy the view?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
If the list is growing and growing then you are not removing items from it?
Also i only can see you are removing the content one ONE specific panel.
Did you remove the panel from the activity before you remove anything from the list?

Only views which are no longer on the activity will be removed.

Best is to upload a small project showing the problem. Hard to help without.
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
thanks for the reply. But I am removing the view from the list (devices_l.RemoveAt(i))and from the panel (devices.Panel.RemoveAllViews). So there should be no reference left to it. The panel is the parent of the view.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So there should be no reference left to it.
Wrong. You HAVE added the views/panel to the activity. As long you do not remove them from the activity they are still living references....

Again: please upload a small project showing the problem if you want further help.
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Ok, so here is a sampleproject. With clicking the left button you add a customview to the panel (there is nothing visible, just a timer tick log). Clicking the right button removes all views from the panel. Yet the timer in the customview keeps firing. So for my understanding I add the view to the panel and remove it. That should remove all references to it and thus destroy it. Or does this somehow work differently?
 

Attachments

  • test3.zip
    10 KB · Views: 70
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
If I add this
Button2:
Private Sub Button2_Click
    Log("-------------------------------")
    For Each v As Object In Activity.GetAllViewsRecursive
        Log(v)
    Next
    Log("...............")
    For Each v As Object In Panel1.GetAllViewsRecursive
        Log(v)
    Next
    Panel1.RemoveAllViews
    Log("...............")
    For Each v As Object In Activity.GetAllViewsRecursive
        Log(v)
    Next
    Log("...............")
    For Each v As Object In Panel1.GetAllViewsRecursive
        Log(v)
    Next
End Sub

I can clearly see that the number of views is reduced by the amount of cv's in the panel. In the panel and in the activity. So the views are removed. But they still somehow exist. How is this possible?

tim_Tick 1
tim_Tick 0
-------------------------------
android.widget.Button{997558d VFED..C.. ........ 60,958-360,1258 #2}
anywheresoftware.b4a.BALayout{903daf V.E...... ........ 0,0-1080,928 #3}
anywheresoftware.b4a.BALayout{7d8d21c V.E...... ........ 0,0-262,262 #5}
anywheresoftware.b4a.BALayout{4fe2e25 V.E...... ........ 0,0-262,262 #6}
android.widget.Button{fb95466 VFED..C.. ...P.... 599,958-899,1258 #4}
...............
anywheresoftware.b4a.BALayout{7d8d21c V.E...... ........ 0,0-262,262 #5}
anywheresoftware.b4a.BALayout{4fe2e25 V.E...... ........ 0,0-262,262 #6}
...............
android.widget.Button{997558d VFED..C.. ........ 60,958-360,1258 #2}
anywheresoftware.b4a.BALayout{903daf V.E...... ......ID 0,0-1080,928 #3}
android.widget.Button{fb95466 VFED..C.. ...P.... 599,958-899,1258 #4}
...............
tim_Tick 1
tim_Tick 0
tim_Tick 1
tim_Tick 0
tim_Tick 1
tim_Tick 0
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
A Timer is not a View.
Panel1.RemoveAllViews removes the user interface objects not the Timer.
Yes, but the timer is part of the cutsomview class. Is it not removed all together? Do I need to add a remove sub that sets all variables to null or how should this be handled?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
If you look at the Intellisense for Timer.Initialize it says

IMPORTANT: this object should be declared in Sub Process_Globals.

You are declaring Timers in your class and not even keeping a reference to it yourself. At a quick guess, because I can't be bothered to think it all through, somewhere a list of pending timer events is kept for the app message queue to dispatch when needed. This is keeping a reference to the timer which is therefore not garbage collected and keeps on going. In turn this event is keeping a reference to the cv instance so it knows which Sender to reference.

You need to put the timers in an Activity Sub Process_Globals and it is good practice, it you are not using B4XPages, top stop the Timer on Pause and start it again in Resume.
 
Upvote 0
Top