Android Question (Solved) UI freezes while sub is running

josejad

Expert
Licensed User
Longtime User
Hi everyone:

I have a problem populating a listview with pics.
I have an activity with a xCustomListView, when I click in an item
- I start another activity.
- Load a tab layout with two tabs, everyone with a xCustomListview.
- Populate the listview, every item is a small description, and another xCustomlistView (horizontal) where I load the pictures I have in a map, inside a round bitmap.

The problem is when I click an item, the UI freezes for a while, then go a black screen, and then after a few seconds I get the activity with the pics.
I want to show a progress dialog with something like "Reading pics" while the pics are loading, but even the progress dialog is not showing.

I've tried with Wait For, but then just one item is created.

32 items must been created, with 5 or 7 pics every item.

thanks in advance

B4X:
Sub CLVSites_ItemClick (Index As Int, Value As Object)
    Dim site As TipoSite
    Starter.SiteActual = Value
    site = Value
    If site.MapFotos.Size = 0 Then
        CallSubDelayed2("Starter","ReadPhotos", site)
    Else
        StartActivity("Checklist")
    End If   
End Sub

Activity Checklist
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("TabDescription")
    TabStrip1.LoadLayout("TabGeneral", "Fotos Generales")
    TabStrip1.LoadLayout("TabEspecificas", Starter.SiteActual.TipoInstalacion)
   
    For Each lbl As Label In GetAllTabLabels(TabStrip1)
        lbl.Width = 50%x
    Next
    Starter.csvGeneral = Starter.csv.LoadCSV(File.DirAssets, "Generales.csv", ",")
    Starter.csvEspecifico = Starter.csv.LoadCSV(File.DirAssets, Starter.SiteActual.TipoInstalacion & ".csv",",")
    PopulateListView(Starter.SiteActual)
End Sub

B4X:
Sub PopulateListView (site As TipoSite)
    Dim indice As Int = 0
    For Each Row() As String In Starter.csvGeneral
        'ProgressDialogShow2("Reading pics " & Row(0), False)
        CreateItemChk(Row(0), site.MapFotos.Get(Row(0)), indice) 
        indice = indice + 1
    Next
    For Each Row() As String In Starter.csvEspecifico
        'ProgressDialogShow2("Reading pics " & Row(0), False)
        Wait for (CreateItemChk(Row(0), site.MapFotos.Get(Row(0)), indice)) Complete
        indice = indice + 1
    Next
    ProgressDialogHide
End Sub

Sub CreateItemChk(Punto As String, ListaFotos As List, index As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    Activity.AddView(p, 0, 0, 100%x, ExpandedHeight)
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("cellPuntosChk")
    p.RemoveView
    For i = 0 To ListaFotos.Size - 1
        Dim pnl As Panel
        pnl.Initialize("")
        Activity.AddView(pnl, 0, 0, 100%x, ExpandedHeight)
        pnl.SetLayout(0, 0, 90dip, 60dip)
        pnl.LoadLayout("ImagenPunto")
        pnl.RemoveView 'remove from parent    
Dim img As B4XBitmap = xui.LoadBitmap(ListaFotos.Get(i))
        IVFotoPunto.SetBitmap(CreateRoundBitmap(img, 70dip))
        IVFotoPunto.Tag = ListaFotos
        pnl.Tag = ListaFotos.Get(i)
        CLVFotos.Add(pnl, ListaFotos.Size)
    Next
    Return p
End Sub
 

josejad

Expert
Licensed User
Longtime User
Thanks both of you!!
Now it's working as expected, just added Sleep(0) after the CreateItemChk sub. (and xui.LoadBitmapResize).

But I have a question.
If I write the ProgressDialogShow before the first "FOR" and ProgressDialogHide at the end of the sub, it works well.
But if I write them inside the loops, to show the items I'm loading, and the ProgressDialogHide at the end of the sub, the ProgressDialog is not shown. If I increase the value of Sleep, then I can see it showing and hiding, but the ProgressDialogHide should not be rise until the end of the loops, due to the ResumableShow, rigth?

Thanks in advance.

B4X:
Sub PopulateListView (site As TipoSite)
    Dim indice As Int = 0
    For Each Row() As String In Starter.csvGeneral
        'ProgressDialogShow2("Reading pics " & Row(0), False)
        CreateItemChk(Row(0), site.MapFotos.Get(Row(0)), indice) 
        Sleep(0)
        indice = indice + 1
    Next
    For Each Row() As String In Starter.csvEspecifico
        'ProgressDialogShow2("Reading pics " & Row(0), False)
        Wait for (CreateItemChk(Row(0), site.MapFotos.Get(Row(0)), indice)) Complete
        Sleep(0)
        indice = indice + 1
    Next
    ProgressDialogHide
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Sorry for not start a new thread, but I have a problem at the same point.
Sleep(0) is refreshing the UI, but after one day working, now I'm getting a strange behaviour.
After the first iteration, it seems "Sleep" or something is resuming the Activity, so some items are getting repeated.

B4X:
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Loading sites
** Activity (main) Resume **
CLVSITES_Itemclick
** Activity (main) Pause, UserClosed = false **
** Activity (checklist) Create, isFirst = true **
Class not found: b4a.example.imageslider, trying: com.semi.bqa.imageslider
PopulateListView
Iteration: 0
** Activity (checklist) Resume **
PopulateListView
Iteration: 0
Sleep not resumed (context is paused): b4a.example3.customlistview$ResumableSub_PanelClickHandler
Iteration: 1
Iteration: 1
Iteration: 2
Iteration: 2
Iteration: 3
Iteration: 3
...

I've been searching about the "Sleep not resumed (context is paused)" and I've seen several possible causes:
- The sleep is reached while the Activity is resumed (not my case)
- A post of Erel saying that if you press back buttom this is the expected behaviour (not my case)

So I don't know whit the Activity called checklist is being resumed.

This happens if I call "StartActivity(Checklist)" from Main activity. But If I call "PopulateListView" from Checklist Activity_Resume, it works fine, so I've tried
CallSubDelayed(Checklist, "PopulateListView") from Main instead "StartActivity", then I get:

B4X:
....
Iteration: 0
Sleep not resumed (context is paused): b4a.example3.customlistview$ResumableSub_PanelClickHandler
Iteration: 1
Iteration: 1
An error occurred: 
(Line: 90) Sleep(0)
java.lang.Exception: Sub populatelistview signature does not match expected signature.
public static void com.semi.bqa.checklist_subs_0._populatelistview(anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception

No parameters

Thanks again
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
I fill the list in activity resume because when I click an item, I call an CameraEx activity, so when I go back to this activity I need to refresh the pics.
I made an xCustomListView.clear before call PopulateListView. When I call the sub from Activity_Resume, it woks fine, it’s not resume and there is no repeated items.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi again:

Thank you. After your comments, I re-read the android life cycle, I was wrong, I thought Activity_Resume was just called after a resume, but I've seen it's called after Activity_Create too.
I've seen this post I haven't seen before, and I've printed the LifeCycle and hanged it in my wall :-D

https://www.b4x.com/android/forum/threads/graphical-life-cycle-of-a-b4a-activity.40515/#post-242231

I've moved the call to PopulateListView(Starter.SiteActual) to Acitivty_Resume and now it's working as expected.

Off topic: Everyday I love more b4x, I'm learning very slowly due to I've got so little time, but there are a lot of resources, the forum is great...
Sometimes I've found my own answers after a lot of testing, and when I decide to post in the forum, I start to clean the code to post, or create a small project to reproduce the failure and... solution found before posting, hehe. (Not this time).

Thanks again to everyone.
 
Last edited:
Upvote 0
Top