Android Question ExitApplication / Activity.Finish do not close App

yonson

Active Member
Licensed User
Longtime User
Hi

I've been trying unsuccessfully to exit my application by implementing the 'press back again to exit' routine.

I've actually written my own small bit of code to do this which I'll share once I've got everything right. However, at the moment I'm not actually able to remove the application from memory.

I've tried using both Activity.Finish (which I know is the correct approach) and ExitApplication, but no matter what I use the app remains in memory and I can still browse to it through the menu bar.

I've assumed that this must be because of a service or something running in the background, but I can't find anything that would be causing this, there are no services although there is a GPS listener, but I disable this when closing and its still in memory.

Does anyone have any suggestions about what might be causing this?

Many thanks
John
 

Cableguy

Expert
Licensed User
Longtime User
This hás been answered so many times!

Check the beginners guide by Klaus, which you can find in his signature.
Check the Activities life-cycle thread!
An app only "terminates" when the OS decides to liberate the used resources!
 
Upvote 0

yonson

Active Member
Licensed User
Longtime User
Hi yes I read this by Klaus, my issue though is that I have other apps in the past that have followed this procedure, and the app closes / is cleaned up by the OS no problem at all. However in the case of this specific app it remains in memory - the OS does not at any point 'liberate' it hence the issue, it is still in memory hours later even after loading up other apps etc
 
Upvote 0

yonson

Active Member
Licensed User
Longtime User
For reference here is the code I use to implement the 'press again to exit' functionality seen in many android apps

To work, set up a 'exitTimer' Timer, and have a tick value of, say, 2 seconds. This is a way to capture the user pressing back twice within 2 seconds.

B4X:
Sub Process_Globals
    ' exit timer, used to time between user pressing back button twice
      Dim exitTimer As Timer
      Dim exitTimer_tick_duration As Int = 2000 ' tick every 2 seconds  
End Sub

Sub exitTimer_Tick
    exitTimer.Enabled=False
End Sub


Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event

If KeyCode = KeyCodes.KEYCODE_BACK Then
  
   If (exitTimer.Enabled=False) Then
      ToastMessageShow("Press Again To Exit",False)
    If (exitTimer.IsInitialized=False) Then
        exitTimer.Initialize("exitTimer",exitTimer_tick_duration)
    
    End If
    exitTimer.Enabled=True
    Return True
    Else
        ' exit app, timer is running and it hasn't 'ticked' round yet by tick interval (2 secs or whatever). so exit now
        gps_ref.Stop
        exitTimer.Enabled=False
        closeApplication=True
      
        Activity.Finish
        ExitApplication
        Return True

      
    End If
End If
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Keep in mind that no code is executed after ExitApplication so your return true is never executed.
If the app uses very little resources then it is possible that it stays in memory for a few hours...
You have two ways of testing this:
Load a very large image i n your app, and see how long it takes to the OS to liberate the resources.
Try heavelly using the device, GPS apps are good at that... It may force the OS to release the resources used by your app
 
Last edited:
Upvote 0
Top