ExitApplication: kill the process making sure all activities are closed

peacemaker

Expert
Licensed User
Longtime User
Is it possible in whole ?

I saw many discussions here about Activity.Finish, but if i really need to make full restart of the app (to update the TImeZone for sure) - i have to kill the process.

But if several Activities and one is opened from another - the system restarts the first Activity. And seems, any global flag "FullExit" is always False, as its module is already closed.

How to make sure to kill app process with several activities?
 

barx

Well-Known Member
Licensed User
Longtime User
Search the forum for

B4X:
ExitApplication
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I mentioned that it's known, and searched for.
Problem is that when current Activity has stopped all the services and used ExitApplication - the system restarts a previous app's Activity.

Or does ExitApplication for sure kill the process even if other services are active ? And prev Activity is started as a new process ?
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I'm not 100% sure. The documentation says it ends the process, though I have never tried it to be honest.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Yes, seems, it's unique situation - always Activity.Finish is OK, but some restrictions of Basic4Android need to re-init the process. But it's impossible, i guess - ExitApplication is useless - simple app with one Activity is closed OK by Activity.Finish, but if Activities are open one from another - seems, the app cannot kill youself (the process) for sure...
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Apparently it kills the Process and then restarts it. I found this. System.exit() is Java for ExitApplication.
System.exit() does not kill your app if you have more than one activity on the stack. What actually happens is that the process is killed and immediately restarted with one fewer activity on the stack. This is also what happens when your app is killed by the Force Close dialog, or even when you try to kill the process from DDMS. This is a fact that is entirely undocumented, to my knowledge.

The short answer is, if you want to exit your application, you've got to keep track of all activities in your stack and finish() ALL of them when the user wants to exit (and no, there is no way to iterate through the Activity stack, so you have to manage all of this yourself). Even this does not actually kill the process or any dangling references you may have. It simply finishes the activities.
I also found this suggestion which uses a Global flag, I've edited it from Java to Basic4android
Hold a flag in Process_Globals to force close the app, in Activity_Resume check if the force close flag is set. If so call ExitApplication. This will cascade through the entire activity stack and finally get the app completely closed.

The quotes are from http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon which is an interesting philosophical read on several topics if you have the time to wade through it.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, Agraham !
But where to store the Global flag ? One flag for each activity ?
I guess, if the single common - it will be reset by a some of the first closing Activities.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Very simple. Just one Boolean set to False. Set it True when you want to kill everything and call ExitApplication. Check it first thing in every Activity_Resume and if it is set then call ExitApplication again. Eventually everything should end after all the activities on the activity stack are stopped. You will need to stop any services. Have a play and see what happens.
 
Upvote 0

gadgetmonster

Active Member
Licensed User
Longtime User
Thanks, Agraham !
But where to store the Global flag ? One flag for each activity ?
I guess, if the single common - it will be reset by a some of the first closing Activities.

I just came across this post so thought I would add how I have done this.

As suggested I added a Boolean variable to my main activity in process_globals called ForceClose.

Then in each of my activities I have a menu item called Close App. When Clicked it calls a function called CloseDown as follows:

B4X:
Sub CloseDown_Click

   Main.forceClose = True
   Activity.Finish 

End Sub

As you can see, this sets the ForceClose Boolean to True and then ends the activity.

Then on each of my activities under Activity_Resume I do this:

B4X:
If Main.forceClose Then Activity.Finish

Finally on my main activity under Activity_Resume I have this:

B4X:
If forceClose Then ExitApplication

Now whenever a user clicks Close App from any activity, no matter how deeply nested, the activities will all close and the app will end.

Hope this helps
 
Upvote 0

DAM

Member
Licensed User
Longtime User
HELP NEEDED

First of all, as always, thanks in advance for your help...
I have an app with a start activity menu (Start). From here, you can go to other activity (Page1) which can send to other(Page2).
Page2 goes to Page1 with an up button in actionbar.
From Page1 you can go to Start with Back button.
In Start, i want to exit with Back Button.

What happens?
I use the solution given by gadgetmonster or Klaus(in another post): putting a variable close which is set to true, etc.

BUT....
Page1 opens as Activity_pause + Activity_resume and even if I have that variable close set to true, you actually can see Page1 activity on the device before it finishes! :(:(:confused:

Ok, the app exits but it is not good that you can see the activities which were paused before it exits...

Please, can you help me??

Thanks a lot!!
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
Thanks GadgetMonster.
And to kill the services?
I stop the service(s) with StopService and then can I use the ExitApplication?

Best regards


gadgetmonster
 
Upvote 0

fash866

Member
Licensed User
Longtime User
Very simple. Just one Boolean set to False. Set it True when you want to kill everything and call ExitApplication. Check it first thing in every Activity_Resume and if it is set then call ExitApplication again. Eventually everything should end after all the activities on the activity stack are stopped. You will need to stop any services. Have a play and see what happens.

Yes!It's a good idea!3ks!
 
Upvote 0

fash866

Member
Licensed User
Longtime User
Very simple. Just one Boolean set to False. Set it True when you want to kill everything and call ExitApplication. Check it first thing in every Activity_Resume and if it is set then call ExitApplication again. Eventually everything should end after all the activities on the activity stack are stopped. You will need to stop any services. Have a play and see what happens.

Yes!It's a good idea!3ks!
 
Upvote 0
Top