Android Question Programmatically added views disappear on pause/resume

warren_davies

New Member
Licensed User
I am writing an app where the user is able to select an image from a listView to add that image to the layout and move it around.

The user clicks on a listview item, and an object is selected to be added as an ImageView, i;
Activity.AddView(i, 240dip, 10dip, 120dip, 120dip)
This works, the user can add ImageViews.

But after a pause/resume, they all disappear. Can I use StateManager to restore the ImageViews?
 

warren_davies

New Member
Licensed User
Create a process global List with the images indices or names and then recreate the ImageViews in Activity_Create. Then call StateManager.RestoreState.

Thanks, I tried this. But decided that to simplify things I would just try and reposition a single known image. The user adds an imageView and changes it's coordinates after adding it. The screen then goes to sleep, and on resume I want the imageView to be at the same coordinates.

This is the code that adds the imageView initially;

B4X:
Sub ListView2_ItemClick (Position As Int, Value As Object)
   
    Dim localGarment As garment
   
    i.Initialize("imageClick")   
    localGarment = garmentMap.Get(Value)
   
    i.Bitmap = LoadBitmapSample(File.DirAssets,localGarment.GetFileName,40dip,40dip)
    Activity.AddView(i,240dip,10dip,120dip,120dip)
       
End Sub


This is the code I use to populate some lists, but also to re-create the single imageView;

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("LayoutLandscape")
   
    ListView1.AddTwoLinesAndBitmap("Het","",LoadBitmap(File.DirAssets,"Purple-hat-300px.png"))
    ListView1.AddTwoLinesAndBitmap("Crys-T","",LoadBitmap(File.DirAssets,"Yellow-T-Shirt-300px.png"))
   
    garmentMap.Initialize   
   
    Dim myGarment As garment
    myGarment.Initialize("Crys 1","Azure-T-Shirt-300px.png")
    garmentMap.Put("Crys1",myGarment)
   
    Dim myGarment As garment
    myGarment.Initialize("Crys 2","Green-T-Shirt-300px.png")
    garmentMap.Put("Crys2",myGarment)
   
    Dim myGarment As garment
    myGarment.Initialize("Crys 3","Violet-T-Shirt-300px.png")
    garmentMap.Put("Crys3",myGarment)
   
    If FirstTime == False Then
        'Recreate a single instance that the user has added for this test, "Crys1"
        Dim localGarment As garment
        i.Initialize("imageClick")
        localGarment = garmentMap.Get("Crys1")
        i.Bitmap = LoadBitmapSample(File.DirAssets,localGarment.GetFileName,40dip,40dip)
        Activity.AddView(i,240dip,10dip,120dip,120dip)
    End If
           
End Sub

This does add the imageView again, but calling stateManager.Restore doesn't seem to change the coordiates of the imageView back to where the user positioned it.

Here are my pause/resume subs;

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    StateManager.SaveState(Activity, "Main")

End Sub

Sub Activity_Resume
    StateManager.RestoreState(Activity, "Main",0)
End Sub
 
Upvote 0
Top