Android Question KeyCodes.KEYCODE_BACK

strupp01

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Activity.Finish
        
        Select Main.Ruecksprungsort
            Case "Ueberweisungen"
                Main.Ruecksprungsort = "Main"
                StartActivity(Ueberweisungen)
            Case "Finanzverlauf"
                Main.Ruecksprungsort = "Main"
                StartActivity(Finanzverlauf)
            Case "Buchung_suchen"
                Main.Ruecksprungsort = ""
                StartActivity(Buchung_suchen)
            Case Else
                Main.Ruecksprungsort = ""
                StartActivity(Main)
        End Select
        Return True
    End If
    Return False
End Sub

is the sub programmed correctly or do you see any programming errors.
Have some problems when Return True and when Return False is used. Is Activity.Finish correct there or does it have to be in every case statement?
Greetings strupp01
 

JohnC

Expert
Licensed User
Longtime User
Try MOVING the Activity.Finished to after then "Return True" line:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        
        Select Main.Ruecksprungsort
            Case "Ueberweisungen"
                Main.Ruecksprungsort = "Main"
                StartActivity(Ueberweisungen)
            Case "Finanzverlauf"
                Main.Ruecksprungsort = "Main"
                StartActivity(Finanzverlauf)
            Case "Buchung_suchen"
                Main.Ruecksprungsort = ""
                StartActivity(Buchung_suchen)
            Case Else
                Main.Ruecksprungsort = ""
                StartActivity(Main)
        End Select
        
        Return True
        
        Activity.Finish
        
    End If
    
    Return False
    
End Sub


 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try removing the activity.finish line totally from the sub.

Normally you would open activities in a stack order, and the natural operation of the back button would be to reverse that stack order by closing the current activity, so the previous activity in the stack is shown.

But you want to conditionally control which activity gets shown when the back button is pressed. So, you will have to manually keep track of your desired order and implement a back-key override routine (like you are doing above) in all relevant activities so it will keep the unstack order you desire.
 
Last edited:
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Thank you for your help. I have to look at it again. Have 25-30 different activities in the program and so far close the current activity when changing so as not to use too much memory.
Greetings strupp01
 
Upvote 0
Top