This is an example of a Tick-Tack-Toe game.
This game demonstrates two concepts. The first is an example of working with multiple views without duplicating code. In this case it is 9 buttons, however similar code can easily handle any number of views.
The second concept is the handling of Activity_Pause and Activity_Resume events to keep the game state during the activity life cycle.
The test it you just need to rotate the device. During the orientation change the activity is paused, created again and eventually resumed.
The game should continue from the previous state.
The following code creates the 9 buttons, puts each button in the correct place and stores a reference to this button in the Buttons array (two dimensions array):
Later in Sub Button2_Click we find the button that raised the event with the help of Sender keyword:
Saving the game state is done by using a process global variable. Unlike regular global variables, these variables are kept even when the activity is recreated.
Note that the Buttons array cannot be declared as a process global variable. Only non-UI objects can be declared in Sub Process_Globals.
The code is available here: http://www.basic4ppc.com/android/files/tutorials/TickTackToe.zip
This game demonstrates two concepts. The first is an example of working with multiple views without duplicating code. In this case it is 9 buttons, however similar code can easily handle any number of views.
The second concept is the handling of Activity_Pause and Activity_Resume events to keep the game state during the activity life cycle.
The test it you just need to rotate the device. During the orientation change the activity is paused, created again and eventually resumed.
The game should continue from the previous state.

The following code creates the 9 buttons, puts each button in the correct place and stores a reference to this button in the Buttons array (two dimensions array):
B4X:
For x = 0 To 2
For y = 0 To 2
Dim b As Button
b.Initialize("button") 'All buttons share the same event sub
b.TextSize = 30
Activity.AddView(b,offsetX + x * (width + 10dip), offsetY + y * (width + 10dip), width, width)
Buttons(x, y) = b 'store a reference to this view
Next
Next
B4X:
Sub Button_Click
'Using Sender we find the button that raised this event
Dim b As Button
b = Sender
If b.Text <> "" Then Return 'Already used button
...
B4X:
Sub Activity_Resume
'restore the previous state when the activity resumes
For x = 0 To 2
For y = 0 To 2
Buttons(x, y).Text = ButtonsText(x, y)
Next
Next
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
'If the user pressed on the back key we cancel the current game
NewGame
End If
'save the current state when the activity is paused.
For x = 0 To 2
For y = 0 To 2
ButtonsText(x, y) = Buttons(x, y).Text
Next
Next
End Sub
The code is available here: http://www.basic4ppc.com/android/files/tutorials/TickTackToe.zip