[HELP] screen orientation

crisjue

New Member
guys help me.. everytime i change the screen orientation, all my inputed value in the textbox are deleted. please help me how this works

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim texta As String
   Dim textb As String
   Dim textc As String
   
   Dim equation1 As Float
   Dim equation2 As Float
   Dim equation3 As Float
   
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 clearbutton As Button
   Dim EditTexta As EditText
   Dim EditTextb As EditText
   Dim EditTextc As EditText
   Dim Label1 As Label
   Dim Label2 As Label
   Dim Label3 As Label
   Dim Label4 As Label
   Dim Label5 As Label
   Dim Label6 As Label
   Dim lblroot1 As Label
   Dim lblroot2 As Label
   Dim quitbutton As Button
   Dim solvebutton As Button
   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   
   Activity.LoadLayout ("main")
   
   If FirstTime = True Then
      ToastMessageShow ("welcome!",False)
   End If
   
   
   
   
   EditTexta.InputType = EditTexta.INPUT_TYPE_DECIMAL_NUMBERS
   EditTextb.InputType = EditTextb.INPUT_TYPE_DECIMAL_NUMBERS
   EditTextc.InputType = EditTextc.INPUT_TYPE_DECIMAL_NUMBERS

End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub



Sub quitbutton_Click
   
   Dim quit As Int
   quit = Msgbox2("Are you sure you want to quit?","","YES","","NO",Null)
   If quit = DialogResponse.POSITIVE Then
   ToastMessageShow("Goodbye!",False)
   Activity.finish
   End If
   
End Sub

Sub clearbutton_Click
   
   EditTexta.Text = ""
   EditTextb.Text = ""
   EditTextc.Text = ""
   lblroot1.Text = ""
   lblroot2.Text = ""

End Sub

Sub solvebutton_Click
   
   texta = EditTexta.Text   
   textb = EditTextb.Text   
   textc = EditTextc.Text   
   
   If IsNumber(texta)=False OR IsNumber(textb)=False OR IsNumber(textc)=False Then
       Msgbox("Please input the Values","Warning!")
       Return
   End If
   
   ' -- computations -- '    
      
      equation1 = Power(textb,2) - (4 * (texta * textc)) 'b2-4ac
      
         If equation1 > 0 Then 
         ToastMessageShow("There are 2 Roots!",False)
         equation2 = (-textb -  Sqrt(equation1)) / (2 * texta)
         equation3 = (-textb +  Sqrt(equation1)) / (2 * texta)
         lblroot1.Text = equation2
         lblroot2.Text = equation3
         
      Else If equation1 = 0 Then
         ToastMessageShow("There is 1 Root!",False)
         equation3 = -textb / (2 * texta)
         lblroot1.Text = equation3
         lblroot2.text = ""
      
      Else If equation1 < 0 Then
         ToastMessageShow("There is an imaginary number in the equation!",False)
         lblroot1.Text = ""
         lblroot2.Text = ""
      End If
      
End Sub



Sub EditTexta_TextChanged (Old As String, New As String)
      
   Dim aline As String
    aline = EditTexta.Text
   
   If aline.Length > 4 Then
       EditTexta.Text = aline.SubString2(0,4)
       EditTexta.SelectionStart = 4
       ToastMessageShow("4 character limit",False)
   End If
   
End Sub

Sub EditTextb_TextChanged (Old As String, New As String)
   
   Dim bline As String
    bline = EditTextb.Text
   
   If bline.Length > 4 Then
       EditTextb.Text = bline.SubString2(0,4)
       EditTextb.SelectionStart = 4
       ToastMessageShow("4 character limit",False)
   End If
   
End Sub

Sub EditTextc_TextChanged (Old As String, New As String)

      Dim cline As String
       cline = EditTextc.Text

   If cline.Length > 4 Then
       EditTextc.Text = cline.SubString2(0,4)
       EditTextc.SelectionStart = 4
       ToastMessageShow("4 character limit",False)
   End If
   
End Sub
 

mangojack

Expert
Licensed User
Longtime User
crisjue .. Have a read here Android process and activity lifecycles
Especially
Sub Activity_Resume and Sub Activity_Pause (UserClosed As Boolean)
Each time the activity moves from the foreground to the background Activity_Pause is called.
Activity_Pause is also called when the activity is in the foreground and a configuration change occurs (which leads to the activity getting paused and then destroyed)

Also
Currency converter example


Especially half way down
State

As discussed in the life cycle tutorial we are required to save and restore the state of the application. In our case the state is made of the values in the text boxes and the current selected currencies.


You could also restrict the orientation to either Landscape or portrait

Cheers mj
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Search the forum for the 'statemanager' code module.

It can save and restore the state of your views.

Martin.

Sent from my HUAWEI U8815 using Tapatalk 2
 
Upvote 0
Top