Android Question Exit Application finally..

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
this question has already been treated in other posts, but I still don't understand whether what happens to me is normal or not. My code is:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed=True Then
        Activity.Finish
    End If
End Sub

Private Sub Exit_Click

    Wait For(ConfirmExit("Confirma exit?")) Complete (Resu As Int)
    If Resu=DialogResponse.POSITIVE Then
        Activity.Finish
    End If
End Sub

When I close the App, it apparently closes, but, as we know, the icon is still present among the "active Apps", or, maybe more properly, among the "most recent apps" (i am not sure of this interpretation, anyway). This is what happens. What I don't understand is that, if I click again on the App Icon, it startes crashing, a first time. At second time, it starts. So I am forced, all the times, to ask my users to use the "Close all Apps" Android command, before to launch again the App. What puzzles me is the fact that commercial Apps don't have this behaviour..
What am I missing? Thanks.
 

RichardN

Well-Known Member
Licensed User
Longtime User
'Recent Apps' is a better description for the list summoned by the III button. If you are unsure of the program state for debugging purposes then go to Settings > Apps and kill it. That is the only way to make sure you are getting a clean start.

....Of course if it has just crashed you are also getting a clean start!
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
'Recent Apps' is a better description for the list summoned by the III button. If you are unsure of the program state for debugging purposes then go to Settings > Apps and kill it. That is the only way to make sure you are getting a clean start.
Hi. No, I am speaking about release version. By the way, as I wrote, commercial Apps don't have such a behaviour. Better, it seems to me that they actually don't close, but stay "alive" on the background.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Android doesn't necessarily destroy your activity on Activity.Finish. It puts it in the background and when you select it from the recent apps list will bring it to the foreground. In this case Activity_Create will not run but Activity_Resume will. The fact that it is crashing means there is something wrong in your code. Add '#BridgeLogger: True' to your Project Attributes and see if you get an exception shown in the logs.

To really close your app do one of the following
B4X:
Dim jo As JavaObject
jo.InitializeContext  
'jo.RunMethod("finishAffinity", Null) ' one way
jo.RunMethod("finishAndRemoveTask", Null) 'another way
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes, my App is not designed to restart after closing... otherwise I didn't put an Exit button.. Old programming concepts .. Of course it crashes, in such conditions. I will try your suggestion. Thanks a lot.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Hi @GiovanniPolese

Your problem is that you don't know if Android has sent your program to the background or terminated it. I suggest that you don't actually need to know.....

It is Android preferred design that when you are at your top level screen, pressing the [Back] key hands the app back to the OS.

The Activity_Pause allows you to save the app state. However there seems to be no imperative to know if the OS has terminated the program or not. Go with the Android design concept.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…