Unload secondary Activities

timo

Active Member
Licensed User
Longtime User
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:
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
 

timo

Active Member
Licensed User
Longtime User
Sorry Erel, I don't understand what you mean. Could you explain me how evaluating so the origin of the call?
If I add a splash screen by a panel on the main (with timer), i play with the two activities and then exit by pressing back from the main,
actvity 2 remains still resident and showed. If i then make a choice again, main is reloaded with his splash screen.
But i don't want to close the app in other situations. I think that the only way to do this is by a flag. Am I missunderstanding you? Thank you
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I meant that in the second activity you can change:
B4X:
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
To:
B4X:
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
        Activity.Finish
End Sub

Now the second activity will be removed from the activities stack.
 
Upvote 0
Top