Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically close an application?

Tags:

android

I am looking for code for a button that completely closes my app.

I tried with some stuff from Google, but my app is still running in the background. I need to close it completely. Is there code that does this?

like image 783
NullPointerException Avatar asked Dec 05 '22 00:12

NullPointerException


1 Answers

Why do you need to really close your app? Assuming it's just a normal app and not running any background services or holding a wakelock (you'd know if you were doing those things), the system does a very good job of task management and will end your app if it's backgrounded and it needs the RAM without any manual intervention. Normally if you just finish() your base Activity this will happen on its own, but there's almost never a reason to do that.

(The only exception to this is if your Application is somehow holding onto references to already-finished Activities, which can cause ugly memory leaks and keep your app from closing normally, but you'd also probably know if you're doing anything fishy with an overridden Application subclass.)

That is: 99% of the time if you want to forcibly close your Application, you either need to fix whatever bug in your code makes you think the system can't handle it on it's own, or you need to reread the documentation on the Android application lifecycle again (because you should have already read this 3 times before you started writing an Android app :)).

like image 79
Yoni Samlan Avatar answered Dec 21 '22 11:12

Yoni Samlan