Android Question Resume app

Carta

Member
I try to explain the problem as clearly as possible.

I have an app with many activities, I would like to make sure that once the app is started and the various activities are open, when the app is closed and then reopened, it resumes the last activity left.

I'll explain better. My app is made up of levels / quizzes.
I would like it to resume from the last level on the next start and not from the main

I heard about the KeyValueStore2 library. is it suitable for that purpose? can you give me an example?

ps. sorry if the title is not appropriate
 

Carta

Member
my project is in b4a. how do i bring it in in b4xpage? with b4xPage I never understood how to switch from one activity to another. if I could cmq to switch to p4xPage..how can I solve the above test?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I never understood how to switch from one activity to another
B4X:
B4XPages.ShowPage("page 2")
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I would like it to resume from the last level on the next start and not from the main.

This is a fundamental part of your app design that should have been sorted out before you started any coding at all. If you did not do this then you may have to rewrite parts of your app, even if you do not change to B4XPages.

Yes, a KeyValueStore is designed for exactly this purpose. You need to save information that describes the state of your app when it is closed, and use this in Main to recreate that state when the app next opens. You also need to plan what happens when the User has completed the final level.

with b4xPage I never understood how to switch from one activity to another

The point of B4XPages is that you never switch from one activity to another, you switch to different pages within the single (Main) activity. You will still need a KVS to remember the app state. It is a good idea, in my opinion, to start learning B4XPages with a simple app. Starting by trying to convert a large complicated app is not a good way to begin.
 
Upvote 0

Carta

Member
This is a fundamental part of your app design that should have been sorted out before you started any coding at all. If you did not do this then you may have to rewrite parts of your app, even if you do not change to B4XPages.

Yes, a KeyValueStore is designed for exactly this purpose. You need to save information that describes the state of your app when it is closed, and use this in Main to recreate that state when the app next opens. You also need to plan what happens when the User has completed the final level.



The point of B4XPages is that you never switch from one activity to another, you switch to different pages within the single (Main) activity. You will still need a KVS to remember the app state. It is a good idea, in my opinion, to start learning B4XPages with a simple app. Starting by trying to convert a large complicated app is not a good way to begin.
thank you, I will listen to your advice!
As far as the KeyValueStore is concerned, could you give me an example of its use?
as I cannot find Italian guides on the web and I have difficulty
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I will try to write a simple example, but it might take a day or two because I am in hospital right now. Should be back to normal tomorrow.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
KeyValueStore
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
first of all I hope nothing serious.
No nothing too serious - I had a cataract removed from one eye. But it means that I had to write the attached example app with one eye covered up, so I apologise for any missed errors.

The example is a single "Main" activity with four other (in this case identical) activities. The four activities are opened in a fixed sequence which I hope represents the puzzles that you have in your app. When the app is restarted it continues from where we left off. The "Main" and "Activity1" activities contain the comments that, I hope, explain how the KVS is used. There are other ways that this could be done so just take it as an example.

Some important points ...

1. The first screen is a "splash" screen. This is not essential - it is there to provide somewhere to put a "Reset" button which makes testing the app much easier.

2. In this case all the activities are the same. That makes it easy to save their "state" in a common object type (and much quicker for me to write) but even if your activites are different it is useful to save all the state variables together in a "type" - this becomes "self-documenting".

3. VERY IMPORTANT - the Android "back button" is a problem. If a player moves from Activity1 to Activity2 then the back button will take them back to Activity1 - this is not whar you want to happen. If they close and restart the app after reaching Activity2 then they will restart in Activity2 and will not be able to return to Activity1 - that is okay. I have not fixed that conflict. This is a good example of a situation where using B4XPages would make it easier to fix that when using activities.
 

Attachments

  • KVSDemo.zip
    14.8 KB · Views: 201
Upvote 0

Carta

Member
No nothing too serious - I had a cataract removed from one eye. But it means that I had to write the attached example app with one eye covered up, so I apologise for any missed errors.

The example is a single "Main" activity with four other (in this case identical) activities. The four activities are opened in a fixed sequence which I hope represents the puzzles that you have in your app. When the app is restarted it continues from where we left off. The "Main" and "Activity1" activities contain the comments that, I hope, explain how the KVS is used. There are other ways that this could be done so just take it as an example.

Some important points ...

1. The first screen is a "splash" screen. This is not essential - it is there to provide somewhere to put a "Reset" button which makes testing the app much easier.

2. In this case all the activities are the same. That makes it easy to save their "state" in a common object type (and much quicker for me to write) but even if your activites are different it is useful to save all the state variables together in a "type" - this becomes "self-documenting".

3. VERY IMPORTANT - the Android "back button" is a problem. If a player moves from Activity1 to Activity2 then the back button will take them back to Activity1 - this is not whar you want to happen. If they close and restart the app after reaching Activity2 then they will restart in Activity2 and will not be able to return to Activity1 - that is okay. I have not fixed that conflict. This is a good example of a situation where using B4XPages would make it easier to fix that when using activities.
I know the problem well, I wish you a quick recovery.

I appreciate your post even more. I also found it very clear and well explained. Thank you very much.

I will try to understand more by looking at your example. I will keep you updated. Thanks again!
 
Upvote 0

Carta

Member
Hi Brian, I have a problem with the activityState :

Level2 - 89: Unknown type: activitystate <br /> Is a reference to a library missing?

Of course I have selected the KVS library.

from your example I don't see other active libraries, what does this error depend on?
 
Upvote 0

Carta

Member
Hi Brian, I have a problem with the activityState :

Level2 - 89: Unknown type: activitystate <br /> Is a reference to a library missing?

Of course I have selected the KVS library.

from your example I don't see other active libraries, what does this error depend on?
My mistake, sorry!
Everything works perfectly. Thanks Bryan
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi Carta.
Level2 - 89: Unknown type: activitystate <br /> Is a reference to a library missing?

This message is referring to an error on line 89 in module "Level2". My app does not have an activity with that name, and none of the activities have that many lines and I use "activityState" rather than "activitystate", so I guess that this is in your own app. Did you remember to define "Type activityState .... .... ...." in Main/Process Globals?
 
Upvote 0

Carta

Member
Hi Carta.


This message is referring to an error on line 89 in module "Level2". My app does not have an activity with that name, and none of the activities have that many lines and I use "activityState" rather than "activitystate", so I guess that this is in your own app. Did you remember to define "Type activityState .... .... ...." in Main/Process Globals?
Yes you are right, I had forgotten this:
tipo ActivityState .... .... .... "in Main / Process Globals

I'm having trouble determining the type of activityState.
Can I send you my project? so you give it a look.
If you like you can leave me an email
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi @Carta . Sorry - I have been busy today and missed your posts. I am afraid that I do not communicate here by e-mail, but if you post your project on this site (using "Export as zip" in the File menu) I will try and take a look at it tomorrow. If it is too big to zip try removing some of the activities temporarily.

My expectation is that your different activites will need to save different data in order to record their states. In that case you will need to define a different set of variables for each activity. It is not essential that these are collected together in an "activityState", but if you do when you come back to this application in the future you will quickly be reminded of which variables are needed to set up the activity.
 
Upvote 0

Carta

Member
Ok Brian, I created a download link, I only left 5 businesses with a few files.

I have only 1 item that needs to be reported for each updated activity.


The data that I have to report to each activity is that of the "numeroPellicole" which varies for each activity according to the aids used.



HERE IS THE LINK FOR THE DOWNLOAD

 
Upvote 0

Carta

Member
Thank you! the only element that must be reported UPDATED is the "numeroPellicole" or the "coins" left at that point of play. Thanks and take your time!

You're giving me a great hand!
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have looked at your app. You are making things very hard for yourself by having so much repetitive code and I spent several hours trying to simplify it. I made some good progress, reducing your project to just two activities (Main and one other handling all of the quiz levels) plus a class module to manage the data. But eventually I decided that I could not, at this late stage, handle the keyboard layouts in a way that I knew would be reliable, so I retreated to a very simple fix.

You can remove all references to "activityState". There are only two values that your KVS needs to remember - the level at which to restart the quiz, and the score so far.

In your Main activity you need these two code blocks ...

In Main.ActivityCreate ...

B4X:
    If FirstTime Then
        kvs.Initialize(xui.DefaultFolder, "appState")
        If (kvs.ListKeys.Size = 0) Then
            kvs.Put("startAt", 1)
            kvs.Put("score", 100)
        End If
    End If


and at the and of the activity ...

B4X:
Private Sub Buttoninfo_Click
    Dim start As Int
    start = kvs.Get("startAt")
    Select start
        Case 2    :    StartActivity(Level2)
        Case 3    :    StartActivity(Level3)
        Case 4    :    StartActivity(Level4)
        Case 5    :    StartActivity(Level5)
        ... etc etc
        Case Else
            StartActivity(Level1)
    End Select
End Sub
B4X:

In every "Levelx" module you need to have these two subroutines at the start ...

B4X:
Sub Activity_Resume
    numeroPellicole.Text = Main.kvs.Get("score")    ' Retrieve the score
End Sub


Sub Activity_Pause (UserClosed As Boolean)
    Main.kvs.Put("score", numeroPellicole.Text)        ' Remember the score
End Sub

... and in the "levelup_click" routine, replacing the "??" with the number of the next quiz level ...

B4X:
        ... ...
        ... ...
        mediaplayer4.Initialize
        mediaplayer4.Load(File.DirAssets,"giusto.wav")
        mediaplayer4.Play
        Oscar.x = y1 + 10                ' Moved from after "StartActivity ..."
'    Add the next two lines ...
        Main.kvs.Put("startAt", ??)     'Update next level number
        Main.kvs.Put("score", numeroPellicole.Text)     ' Remember the score
        StartActivity(Level??)        ' Next level activity name
        ... ...
        ... ...

If this is not clear then come back to me. Good luck.
 
Last edited:
Upvote 0
Top