Android Question Issue: Sleep not resumed (context destroyed)

asales

Expert
Licensed User
Longtime User
I tried to download more then 500 images and show it in a CustomListView. The urls of the images is in an database.

I used the code "DownloadImage" of this thread
https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/#content
B4X:
Sub CreateCustomList
    dbCursor.Position = 0
    For i = 0 To dbCursor.RowCount - 1
        dbCursor.Position = i
        clv.Add(CreatePanel(i), 70dip, i)
    Next
End Sub

Sub CreatePanel(i As Int) As Panel
    Dim p As Panel
    p.Initialize("panel")
    Activity.addview(p,0,0,100%x,100%y)
    p.LoadLayout("cellprofile")
    p.RemoveView

    lbName.Text = dbCursor.GetString("name")
    DownloadImage(dbCursor.GetString("image"), ivPhoto)
   
    Return p
End Sub
The images is are display correctly, but the images of the first records take a long time to display, before the others.

I want to the images of the first record is display first and I put a "If mod 10 = 0 then Sleep(100)" in the code, to paused the display every 10 records:
B4X:
Sub CreateCustomList
    dbCursor.Position = 0
    For i = 0 To dbCursor.RowCount - 1
        dbCursor.Position = i
        clv.Add(CreatePanel(i), 70dip, i)
       
        If i Mod 10 = 0 Then Sleep(100)
    Next
End Sub

This worked (the first images is display), but when I try to close the activity don't works. The activity is restarted and I get the this message in the log:
B4X:
Sleep not resumed (context destroyed): com.myapp.listprofiles$ResumableSub_CreateCustomList
How I can fix this problem and show the images of the first records in sequence?

Thanks in advance for any tip.
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
You got this issue because activity in which you call the sleep is closed. You can instead download the image in a service.
Another solution is download the image and wait the finish of image, instead using a sleep.
You can use also another sub to download images: Put the image inside a list, call it in a general sub that await download (you can use this to avoid to change download image code). If you use a list that hold new image dinamically, use a while loop instead a for loop.
 
Upvote 0
Top