Exit.Application and Activitiy.Finish

moster67

Expert
Licensed User
Longtime User
In a different thread, Erel wrote:

I recommend you to call Activity.Finish instead. ExitApplication kills the process. As you see the OS might try to restart it.

I have been using Exit.Application in one of my apps and indeed sometimes the app restarts. I then changed this into Activity.Finish and this problem vanished.

However, there are some drawbacks with Activity.Finish which creates some problems such as app and some resources not being closed immediately and therefore, although not recommended, I would much prefer using Exit.Application.

Can someone explain to me why the OS might try to restart the app (process), technically speaking when using Exit.Application? Is it due to some message-queue or similar? Any way to change this behavior?
 

moster67

Expert
Licensed User
Longtime User
Which resources are not released? In almost all cases it is better to let the OS decide when to kill the process.

If you do want to call ExitApplication then you should make sure that you first stop all services and activities and then call ExitApplication.

Well, my app uses a wrapped external library and despite using its Release-method, some times (not always) the resource is not released and this means that a satellite-receiver and its tuner remains occupied and the satellite-receiver cannot be used.

This did not happen when I used Exit.Application but with Activity.Finish I have this unfortunate behavior. Probably Exit.Application ensures that indeed all processes are killed but some times it makes the app restart :)
 
Upvote 0

AllyAndroid

Member
Licensed User
Longtime User
I'm new here and have only been using Basic4Android for a few days but have run into a similar problem.

I have two activities: Main and Home.

I wanted to give the user the ability to either Logout and go back to Main or Exit the app completely. If they chose to Logout, I was using Activity.Finish. If they wanted to Exit the app, I was using ExitApplication.

However, regardless of what they selected, Main would always restart.

Here is my solution that so far has worked for my app:

For the Main module:

I added to the Sub Process_Globals:

B4X:
Public killApp As Boolean

Then I added this code to the Sub Activity_Create(FirstTime As Boolean):

B4X:
If killApp = True Then
   Activity.Finish
End If

For the Home module:

I added this code:

B4X:
Sub btnLogout_Click
   If Msgbox2("Do you want to close the app?", "Close App","Yes","","No", Null)  = DialogResponse.POSITIVE Then
      Main.killApp = True
    Else
      Main.killApp = False
    End If

   Activity.Finish
End Sub
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
When TimeZone is changed when app (using time code constantly) is working - it needs to restart app for sure. So ExitApplication is required, but it needs to use it at each Activity start...i think.
 
Upvote 0

mesutaslan

Member
Licensed User
Longtime User
ExitApplication will kill the process. If there is any service running then the process will be restarted. You should close all activities and services before calling it.


How can i close all activites and services from 2.activity ?

i wantto make a splash screen show for 5 secs. from begining of application.

i make main activity as splash screen and after 5 secs. call the menu activity.

in menu activity i catch the back key and prompt user "do you wantto exit ?" if negative "return true"

if positive i wantto kill app but it restarts it self..

i am confisued but how can i kill all running procsess or services or etc. ?

Okey, i handle it with this way :

in main activity after i call 2nd activity with "StartActivity" i put the "Activity.Finish" so main activity finish itself and 2nd activity exit when user say "YES"
 
Last edited:
Upvote 0
Top