Android Question How to Exit App from Child Activity?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have 3 activities, Main Activity, and 2 child activity.

Here are the codes snippet in Main Activity
B4X:
Sub Process_Globals
    Dim SqlLt As SQL
End Sub

Sub Button1_Click
    StartActivity(Child1)     
End Sub

Sub Button2_Click
    StartActivity(Child2)     
End Sub

When Child2 Activity is active and user pressed back key, I want to quit from app, not return to Main Activity.

I tried these codes on Child2 Activity, but no avail
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 
   If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
   End If
End Sub

If I changed codes for Button2_click on Main Activity to these one
B4X:
Sub Button2_Click
    Activity.Finish
    StartActivity(Child2)   
End Sub

It works as intended, but is this the correct procedure?
Thanks in advance.
 
Last edited:

Shahid Saeed

Active Member
Licensed User
Longtime User
Hi guys,

I have 3 activities, Main Activity, and 2 child activity.

Here are the codes snippet in Main Activity
B4X:
Sub Process_Globals
    Dim SqlLt As SQL
End Sub

Sub Button1_Click
    StartActivity(Child1)    
End Sub

Sub Button2_Click
    StartActivity(Child2)    
End Sub

When Child2 Activity is active and user pressed back key, I want to quit from app, not return to Main Activity.

I tried these codes on Child2 Activity, but no avail
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
   If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
   End If
End Sub

If I changed codes for Button2_click on Main Activity to these one
B4X:
Sub Button2_Click
    Activity.Finish
    StartActivity(Child2)  
End Sub

It works as intended, but is this the correct procedure?
Thanks in advance.

Yes this is the correct way to do that:

B4X:
Sub Button2_Click
 Activity.Finish 'this will close the main activity
 StartActivity(Child2) ' this will start the Child2 activity
End Sub

Now if you close Child2 activity either by a button_click or back key press it will close the Application.
 
Upvote 0
Top