Android Question How to make checked boxes save to stay checked?

Nb1320

Member
Licensed User
Longtime User
Hello,
I have been a B4A customer for a really long time, but I have never actually finished an app that I started on, due to several reasons, but NOW! I am ready! I have a simple question that I am hoping someone could help me with.

The app is essentially just a long checklist of items, but there are 2 check boxes for each item, "own, want." I just want to be able to make it save whether one or both boxes were checked and the ability to uncheck one or both in the future. This is probably a really easy solution or really difficult, but i'm pretty sure my lack of knowledge is making me overthink everything.

I appreciate all help, thank you very much!

I do not want anyone doing the work for me, but a code snippet or just some kind of advice would be a great help! Thanks!
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
You probably would take a look
- or on KeyValueStore : https://www.b4x.com/android/forum/t...ple-and-efficient-key-value-data-store.26317/
- or on PreferenceActivity (I do use it often even without any screen or categories only to store quickly) : https://www.b4x.com/android/forum/threads/preferenceactivity-library.10609/
- or on StateManager : https://www.b4x.com/android/forum/t...android-applications-settings-and-state.9777/

That's easy : save the checkbox checked state on Activity_Pause (or when it changes) and recover it on Activity_Resume
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Have a look at the statemanager
 
Upvote 0

Nb1320

Member
Licensed User
Longtime User
Thank you lemonisdead for the links and DonManfred for the reassurance. I did in fact look up StateManager and I am about 90% sure that I can edit the demonstration provided to fit my needs. Thank you both very much!
 
Upvote 0

Nb1320

Member
Licensed User
Longtime User
Okay after staring at this for a couple days, I am not sure of what I am doing. I want to explain my app a little more so that I can make sure that the before mentioned tools will work for me.

My app has several activity modules containing one long list on each module. Each "touchable" list item opens a .bas and that's where the check box is located. I looked at KVS, but with my lack of knowledge I depend on the examples to help explain everything to me as I go. The KVS example just starts a smiley face picture and it never goes away no matter what I do. I also looked at PreferenceActivity, but I could not get it to do what I wanted. I'm not sure if that's because the screen was made in designer mode or what. I thought StateManager was going to be the solution, I spent most of my time on this one. I started going through the posts on that library when I couldn't figure it out and now i'm not even sure if it will work because each item opens a new .bas screen.

I apologize if this is confusing, just really want to get somewhere with this, I appreciate all the help. If you have a simple list of items with a checkbox on each line that saves the checks when you open/close the app I will edit it for my needs, lol. Thanks again!

I'm sure there are better ways to do the things I have explained, but I am going about it the best way that I can.;)
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
How is the list of items (items with their checkboxes) created please ? Are they only checkboxes you had put on ScrollViews or do you use UltimateListView or something else ?
 
Upvote 0

Nb1320

Member
Licensed User
Longtime User
I initially had it setup so that there was a listview and each item on the list would open a different .bas screen. On this new opened screen is where the checkboxes were. Now I just want to find a way to make it work and I can update it in the future so that its the way that I really want it. I settling, sadly:(
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Excuse me, but it wasn't so clear. Reason why I had asked more information about. So, if I do well summarize : you have different Activities (you name them .bas) and on those Activities you have two checkboxes (own and want).
So, if I have well understood, the way to go is DonManfred's choice : StateManager
It's very easy to manage. Have you tried the code sample that Erel provided ? If yes, have you noticed the way to save all the activity's views using StateManager.SaveState(Activity, "Main") where "Main" is the ID

Check the example attached. It has a Main and a Second activity. The Checkboxes states are stored when the activity is paused and restored when it is resumed

Hope this can help (and hope to have well understood the need)
 

Attachments

  • example.zip
    16.1 KB · Views: 170
Upvote 0

Nb1320

Member
Licensed User
Longtime User
I hope you know I appreciate all your help. I think I have been correct the whole time, I understand how it works, but the way I want it to work is not achievable. I am sorry that my posts are not more clear, not sure how to explain everything or exactly what everything is called. I am not trying to insult your intelligence, I just want to try to be as clear as possible so that you can help.


I go to Project and make a new Activity Module called "List", in that module there is a list of items, for this example, we'll say numbers. So the list is 1-10 and the user can click on any number from 1-10 to open a page that was made in designer. So this means there are 10 pages that were made in designer that can be opened from the one Activity Module "List".

I understand your example and I VERY MUCH appreciate it! I think my issue is trying to open and keep track of several designer pages within one Activity Module. If this makes anymore sense and you know another way, I would appreciate it. The only way I can see it working with what I know is to make 10 Activity Modules, which is not something I really want to do because there are going to be hundreds of items on the list, not just 1-10. Thanks!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
why not use a "temp" file as a buffer? add a time stamp to be sure the info is not "too old".
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
in activity pause, create/overwrite a temp file with all the "states" you need saving, the in activity resume, read it and set those states to the file values.
it may not be the fastest, but it will allow you to set "indirect" activities items as well... the timestamp you could add as a first line in order to know how old the file is, if its just a few seconds, then act on it, if not discard the info
 
Upvote 0

Nb1320

Member
Licensed User
Longtime User
Cableguy, if I understand you correctly, you're saying to make it like this?

B4X:
Sub Activity_Resume
    StateManager.RestoreState(Activity,"Main1, Main2, Main3",0)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    StateManager.SaveState(Activity,"Main1, Main2, Main3")
End Sub

Correct?

The only thing, if that's right is that I have multiple designer made files, that references the Activity Module, not designer made files. That brings up the issue that I had before with creating hundreds of Activity Modules, when designer made files are easier.

Honestly, I started rethinking the project and I think I can do it a different way to make the StateManager work the way its intended. It'll cause me to start over, but at least its done properly. If that's the route I have to take it'll be okay, I just won't release anything that I am not happy with. I thank you both for all of the help.
 
Upvote 0
Top