Android Question Memorize the value of the checkbox

Isac

Active Member
Licensed User
Longtime User
Hi,
I can not store the true value of the checkbox, when I press on the checkbox the checked value becomes true, but when I restart the app, in the service starter module I always see a false value.

thank you


Here is the test code



Main

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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.

    Public CheckBox1 As CheckBox
 
   

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("io")
   
    If StateManager.RestoreState(Activity, "Main", 0) = False Then
   
       
   
    End If
 


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        StateManager.ResetState("Main")
    Else
        StateManager.SaveState(Activity, "Main")
    End If
    StateManager.SaveSettings
End Sub


Sub CheckBox1_CheckedChange(Checked As Boolean)
   
Starter.test=Checked
Log("Checked="&Checked)
   
End Sub

--------------------------------------------------------------------------
Starter


B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

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

Public test As Boolean

End Sub

Sub Service_Create
        'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
If test = True Then
Log("TRUE")
Else
Log("FALSE")
End If

End Sub

Sub Service_Start (StartingIntent As Intent)
 

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
StateManager.ResetState("Main")
Else
StateManager.SaveState(Activity, "Main")
End If
StateManager.SaveSettings
End Sub
when the user presses back key your state is reset
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Hi,

I tried to use
B4X:
StateManager.SaveState (Activity, "Main")
but it is the same.

the checkbox is stored, but the value checked remains always false when I restart the app.
 
Upvote 0

npsonic

Active Member
Licensed User
Use KeyValueStore and do something like this.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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.

    Public CheckBox1 As CheckBox
 
 

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("io")
    If Starter.kvs.ContainsKey("CheckBox1") Then CheckBox1.Checked = Starter.kvs.Get("CheckBox1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub CheckBox1_CheckedChange(Checked As Boolean)
 
    Starter.kvs.Put("CheckBox1",Checked)
    Log("Checked="&Checked)
 
End Sub

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

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

     Public kvs As KeyValueStore

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

     kvs.Initialize(File.DirInternal,"kvs")

     If kvs.ContainsKey("CheckBox1") Then
         Log(kvs.Get("CheckBox1"))
     Else
         Log("false")
     End If

End Sub

Sub Service_Start (StartingIntent As Intent)
 

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0
Top