Referring to the 'twoactivities' tutorial by Erel, i noticed thet when back button was pressed from the main activity, activity2 was then showed again. Sometimes, especially if you have a splash screen on the main, you prefer that back button closes the hole program without giving the possibility to reload the main from activity2. This is my solution by putting a flag. I marked 'added' for added code to Erel's example:
Main:
Activity2:
Main:
B4X:
Sub Process_Globals
'added:
Dim flagExit As Int : flagExit=0 'controller set to 0
'end added
End Sub
Sub Globals
Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub
Sub Activity_Resume
If Activity2.result.Length > 0 Then
Label1.Text = "You have chosen: " & Activity2.result
Else
Label1.Text = "Please press on the button and choose an item."
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
StartActivity(Activity2)
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'added
'To change the controller
If KeyCode = KeyCodes.KEYCODE_BACK Then
flagExit=1 'this will be controlled in Activity2 resume
StartActivity(activity2)
End If
End Sub 'end added
Activity2:
B4X:
Sub Process_Globals
Dim result As String
result = ""
End Sub
Sub Globals
Dim ListView1 As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("2")
For i = 1 To 100
ListView1.AddSingleLine("Item #" & i)
Next
End Sub
Sub Activity_Resume
'added:
If main.flagExit=1 Then 'if back was pressed on main activity
main.flagExit=0 'important: If the prg reside in memory you will allways have it set to 1 and
'you will be unable to do anything!
activity.Finish 'now discharge all without showing again activity2
End If
'end added
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
result = value 'store the value in the process global object.
StartActivity(Main) 'show the main activity again
End Sub