activity and orientation change

Sw Controll Group Kft

Member
Licensed User
Longtime User
Hi!

I have a problem with activity pause and resume when orientation is changes.
I have a filled scrollview in the first activity and I call the second activity by clicking on the scrollview item. If the orientation of the phone is changed at the second activity, this click does absolute nothing. I fill the scrollview in the activity_resume.

the call route:

B4X:
Sub view_longclick
'start second activity with clicked item:
Dim Send As View
   Send=Sender
   main.beteg=mm.GetValueAt(send.Tag)
   
   StartActivity("adat") 
   'when phone orientation is changes in second activity
   'this command do absolute nothing without kereskep_click!!!!!!!!!!!

End Sub

If I tested this startactivity command via debugger step by step, the second click it is good but without debugger is not.

I tried the using statemanager, activity.finish and so on but I have no result.

Sorry my poor english. Can anybody to help?
 

Sw Controll Group Kft

Member
Licensed User
Longtime User
I have no problem with fill the scrollview. I MUST do this at the activity_resume because after change then orientation the scrollview will be empty:

sub activity_resume
ret=StateManager.RestoreState(Activity, "firstactivity", 60)
'''the searchtext is ready
searchdata(searchtext.text) 'fills the scroll
end sub

My problem is in the view_longclick routine: then startactivity("adat") does no operates after return from second activity when changed orientation.
end sub
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I have no problem with fill the scrollview. I MUST do this at the activity_resume because after change then orientation the scrollview will be empty

See this page in the Documentation Wiki regarding what belongs in _Create and what belongs in _Resume.

As a general rule, all that belongs in _Resume are Timers, etc., which you turned off in _Pause. The reason is explained in the Wiki. In short -- if Android stops your app and restarts it, such as when the orientation of the device changes, your app will run Activity_Create as well as Activity_Resume. But if your app pauses and returns *without* restarting, such as when you press Home (usually), only Activity_Resume will run.

So if you fill the scrollview in _Resume and the app has NOT restarted, it will add data even though the original data is still in it.

If you fill the scrollview in _Create, and the app has not restarted, _Create will not execute and the scrollview will still have the original data in it. If the app HAS restarted, then _Create WILL execute and the scrollview will be filled.

You can usually program around any problems caused by putting such code in _Resume, but what's the point? It's more efficient to simply put such code in _Create where it belongs.
 
Upvote 0

Sw Controll Group Kft

Member
Licensed User
Longtime User
Yes, I've read the Wiki and I think my code should be good.

This is the first activity :

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim kereskep,al1,al2,tp1,tp2,kg1,kg2,kr1,kr2,hiba,message As ImageView
   Dim kerestext As EditText
   Dim scr As ScrollView
   Dim hc As HttpClient
   Dim panel0 As Panel
   Dim st As StringUtils
   Dim Cursor As Cursor
   Dim mm As Map
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
mm.Initialize 'map for the database data

panel0.Initialize("panel0")
hc.Initialize("hc")

activity.LoadLayout("packeres")
scr.Initialize(0) 
panel0=scr.Panel
kerestext.Initialize("kerestext") 'search text
kereskep.Width=32dip   'do the search from the database and fills scrollview
kereskep.Left=100%x-kereskep.Width-1dip
kereskep.Top=7dip
kereskep.Height=40dip
activity.AddView(kerestext,5dip,10dip,kereskep.Left-5dip,40dip)
activity.AddView(scr,0,48dip,100%x,100%y-48dip)
panel0.Color=Colors.LightGray
End Sub

Sub Activity_Resume
ret=StateManager.RestoreState(Activity, "packeres", 60)
'kerestext.Text=main.keres
If kerestext.Text<>"" Then    pactolt 'fill scrollview
End Sub

Sub Activity_Pause (UserClosed As Boolean)

If UserClosed Then
        StateManager.ResetState("packeres")
    Else
        StateManager.SaveState(Activity, "packeres")
    End If
    StateManager.SaveSettings
End Sub
Sub pactolt
   If kerestext.Text.Length<3 Then
      'error:too short data
      ered=Msgbox2("Kérem, legalább három betűt adjon meg!", "Rövid adat","Rendben","","",hiba.Bitmap)
      Return      
   End If
'remove all view from panel0
   For i = panel0.NumberOfViews - 1 To 0 Step -1
        panel0.GetView(i).RemoveView
    Next
   mm.Clear
   panel0.Height=0
   ProgressDialogShow("Beolvasás")   
   If main.elltipus=1 Then 'two way to read data
      keres="select * from paciens where nyersnev like '" & kerestext.Text & "%' order by nev;"
   Else
      keres="select * from paciens where nyersnev like '" & kerestext.Text & "%' and vangond=1 order by nev;"
   End If
   Cursor = Main.SQL1.ExecQuery(keres)
   Dim paneltop As Int
   paneltop=0
   
      For i = 0 To Cursor.RowCount - 1
      Cursor.Position = i
      'put tt to mm map:
      Dim tt As beteg1
       tt.id=Cursor.GetInt("_id")
      tt.nev=Cursor.GetString("nev")
      tt.szuldat=Cursor.GetString("szuldat")
      tt.taj=Cursor.GetString("taj")
      tt.irsz=Cursor.GetString("irsz")
      tt.varos=Cursor.GetString("varos")
      tt.cim=Cursor.GetString("cim")
      tt.kgy=Cursor.GetString("kgy")
      tt.regszam=Cursor.GetString("regszam")
      tt.naplo=Cursor.GetString("naplo")
      tt.tol=Cursor.GetString("tol")
      tt.ig=Cursor.GetString("ig")
      tt.felmod=Cursor.GetString("felmod")
      tt.jelleg=Cursor.GetString("jelleg")
      tt.szuntmod=Cursor.GetString("szuntmod")
      tt.diag=Cursor.GetString("diag")
      tt.km=Cursor.GetString("km")
      tt.neme=Cursor.GetString("neme")
      mm.Put(Cursor.Getstring("_id"),tt)
      '''''''''''''''''''''''
      'fill scrollview panel:
      Dim panel1 As Panel
      panel1.Initialize("View")
      
      Panel0.AddView(Panel1,0,PanelTop,scr.Width,50dip)
      panel1.Color=Colors.Black
      Dim l1,l2 As Label
      Dim kep1,kep2,kep3,kep4 As ImageView
      kep1.Initialize("View")
      kep2.Initialize("View")
      kep3.Initialize("View")
      kep4.Initialize("View")
      kep1.Tag=i & ""
      kep1.Gravity=Gravity.LEFT+Gravity.CENTER_VERTICAL
      If tt.naplo="" AND Cursor.Getstring("ig")="" Then
         kep1.Bitmap=tp1.Bitmap
      Else
         kep1.Bitmap=tp2.Bitmap
      End If
      If Cursor.GetString("vankr")="0" Then
         kep2.Bitmap=kr1.Bitmap
      Else
         kep2.Bitmap=kr2.Bitmap
      End If
      If tt.kgy="0" Then
         kep3.Bitmap=kg1.Bitmap
      Else
         kep3.Bitmap=kg2.Bitmap
      End If
      If Cursor.GetString("vanall")="0" Then
         kep4.Bitmap=al1.Bitmap
      Else
         kep4.Bitmap=al2.Bitmap
      End If

      kep2.Tag=i & ""
      kep2.Gravity=Gravity.LEFT+Gravity.CENTER_VERTICAL
      kep3.Tag=i & ""
      kep3.Gravity=Gravity.LEFT+Gravity.CENTER_VERTICAL
      kep4.Tag=i & ""
      kep4.Gravity=Gravity.LEFT+Gravity.CENTER_VERTICAL

      Panel1.AddView(kep1,0,5dip,16dip,25dip)
      Panel1.AddView(kep2,16dip,5dip,16dip,25dip)
      Panel1.AddView(kep3,32dip,5dip,16dip,25dip)
      Panel1.AddView(kep4,48dip,5dip,16dip,25dip)
      Panel1.Tag=i & ""
      l1.Initialize("View")
      Panel1.AddView(l1,66dip,5dip,100%x-66dip,25dip)
      l2.Initialize("View")
      l1.Tag=i & ""
      l2.Tag=i & ""

      l1.TextSize=16
      l2.TextSize=14
      l1.TextColor=Colors.Yellow
      l2.TextColor=Colors.White
      l1.Text=tt.nev & ", TAJ: "& tt.taj & ", Szül.: "& tt.szuldat
      l2.Text=tt.irsz & " " & tt.varos & " " & tt.cim

      llh1=st.MeasureMultilineTextHeight(l1,l1.Text)
      l1.Height=llh1
      Panel1.AddView(l2,66dip,llh1,100%x-66dip,50dip)
      llh2=st.MeasureMultilineTextHeight(l2,l2.Text)
      l2.Height=llh2
      panel1.Height=llh1+llh2

      
      paneltop=paneltop+llh1+llh2+1dip
   
      Next
      Cursor.Close
      ProgressDialogHide
If paneltop+100dip<=activity.Height Then
   panel0.Height=paneltop+1dip
 Else
   panel0.Height=paneltop+30dip
 End If
'the scrollview is filled
End Sub

Sub kereskep_click
'begin search on kereskep imageview
main.keres=kerestext.Text
pactolt
End Sub
Sub view_longclick
'start second activity with clicked item:
Dim Send As View
   Send=Sender
   main.beteg=mm.GetValueAt(send.Tag)
   

   StartActivity("adat") 
   [B]'when phone orientation is changes in second activity 
   'this command do absolute nothing without using kereskep_click (filling scrollview again) !!!!!!!!!!![/B]

End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 
    'end activity
   If KeyCode = KeyCodes.KEYCODE_BACK Then
         Return False
   End If
End Sub

and the code of second activity:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim scr As ScrollView,panel0 As Panel
   
   Dim nevlabel As Label,paneltop As Int
   Dim cim1,cim2,cim3,cim4 As Label
   Dim st As StringUtils
   Dim Cursor As Cursor
End Sub
Sub labelallit(lab As Label,grav As Int,tface As Typeface,tsize As Int,szin As Int)
lab.Gravity=grav
lab.Typeface=tface
lab.TextSize=tsize
lab.TextColor=szin
End Sub

Sub Activity_Create(FirstTime As Boolean)
'view objects and texts
panel0.Initialize("p1")
scr.Initialize(500)

activity.LoadLayout("adat")
activity.Title="Páciens adatai"
activity.AddView(scr,0,0,100%x,100%y)


nevlabel.Initialize("p1")
rtaj=main.beteg.taj
If rtaj.Length=9 Then
   rtaj=rtaj.SubString2(0,3) & "-" & rtaj.SubString2(3,6) & "-" & rtaj.SubString2(6,9)
End If
nevlabel.Text=main.beteg.nev&", TAJ: "&rtaj&" (szül.: "&main.beteg.szuldat&")"
labelallit(nevlabel,Gravity.CENTER+Gravity.CENTER_VERTICAL,Typeface.DEFAULT_BOLD,14,Colors.Green)
''''''''
cim1.Initialize("p1")
cim1.Text="Krónikus betegségek"
labelallit(cim1,Gravity.CENTER+Gravity.CENTER_VERTICAL,Typeface.DEFAULT_BOLD,14,Colors.Yellow)
''
cim2.Initialize("p1")
cim2.Text="Állandó gyógyszerek"
labelallit(cim2,Gravity.CENTER+Gravity.CENTER_VERTICAL,Typeface.DEFAULT_BOLD,14,Colors.Yellow)
''
cim3.Initialize("p1")
cim3.Text="Táppénz"
labelallit(cim3,Gravity.CENTER+Gravity.CENTER_VERTICAL,Typeface.DEFAULT_BOLD,14,Colors.Yellow)
''
cim4.Initialize("p1")
cim4.Text="Közgyógyellátás adatai"
labelallit(cim4,Gravity.CENTER+Gravity.CENTER_VERTICAL,Typeface.DEFAULT_BOLD,14,Colors.Yellow)
''

End Sub

Sub Activity_Resume
'read from database to scrollview
ProgressDialogShow("Beolvasás")
panel0=scr.Panel
paneltop=0
Dim panel1 As Panel
panel1.Initialize("p1")
panel0.AddView(panel1,0,0,100%x,30)
panel1.AddView(nevlabel,0,0,100%x,30)
llh=st.MeasureMultilineTextHeight(nevlabel,nevlabel.Text)
nevlabel.Height=llh
panel1.Height=llh
paneltop=paneltop+llh
'''''''''''''''''''''''
kronbe(panel1)
allbe(panel1)
tapbe(panel1)
kgybe(panel1)




panel0.Height=paneltop
scr.Height=panel0.Height
ProgressDialogHide
'end read procedure
End Sub
Sub kronbe(pp As Panel)
keres="select bnokod,bnonev from bm,bno where _pacid=" & main.beteg.id & " and bm.diagkod=bno.bnokod order by bnokod;"
Cursor = Main.SQL1.ExecQuery(keres)
If Cursor.RowCount>0 Then
   pp.AddView(cim1,0,paneltop,100%x,30)
   llh=st.MeasureMultilineTextHeight(cim1,cim1.Text)
   cim1.Height=llh
   pp.Height=pp.Height+llh
   paneltop=paneltop+llh
   For i=0 To Cursor.RowCount-1
      Cursor.Position=i
      Dim l1,l2 As Label
      l1.Initialize("l1")
      l2.Initialize("l2")
      labelallit(l1,Gravity.LEFT+Gravity.CENTER_HORIZONTAL,Typeface.DEFAULT_BOLD,14,Colors.White)
      labelallit(l2,Gravity.LEFT+Gravity.CENTER_HORIZONTAL,Typeface.DEFAULT,14,Colors.White)
      l1.Text=Cursor.GetString("bnokod")
      l2.Text=Cursor.GetString("bnonev")
      pp.AddView(l1,0,paneltop,60dip,30)
      pp.AddView(l2,60dip,paneltop,100%x-60dip,30)
      llh=st.MeasureMultilineTextHeight(l2,l2.Text)
      l2.Height=llh
      pp.Height=pp.Height+llh
      paneltop=paneltop+llh
   Next
End If
Cursor.Close
End Sub
Sub allbe(pp As Panel)
pp.AddView(cim2,0,paneltop,100%x,30)
llh=st.MeasureMultilineTextHeight(cim2,cim2.Text)
cim2.Height=llh
pp.Height=pp.Height+llh
paneltop=paneltop+llh
End Sub
Sub tapbe(pp As Panel)
pp.AddView(cim3,0,paneltop,100%x,30)
llh=st.MeasureMultilineTextHeight(cim3,cim3.Text)
cim3.Height=llh
pp.Height=pp.Height+llh
paneltop=paneltop+llh
End Sub
Sub kgybe(pp As Panel)
pp.AddView(cim4,0,paneltop,100%x,30)
llh=st.MeasureMultilineTextHeight(cim4,cim4.Text)
cim4.Height=llh
pp.Height=pp.Height+llh
paneltop=paneltop+llh
End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    'return to first activity
   If KeyCode = KeyCodes.KEYCODE_BACK Then
         Return False
   End If
End Sub

What can I do?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have no problem with fill the scrollview. I MUST do this at the activity_resume because after change then orientation the scrollview will be empty:

sub activity_resume
ret=StateManager.RestoreState(Activity, "firstactivity", 60)
'''the searchtext is ready
searchdata(searchtext.text) 'fills the scroll
end sub

Actually it is better to restore the state in Activity_Create. It was a mistake in the example in the first version of StateManager.
 
Upvote 0

Sw Controll Group Kft

Member
Licensed User
Longtime User
Thanks Erel but the mistake is here....
It is very interesting that I have two other activity in this project where my method is good. Another interesting: recalling the first activity with breakpoint in debug and step by step the program is correct, otherwise not.

Why?
 
Upvote 0
Top