- What is the right way to close apps in Android?
- 6 Answers 6
- Activities don’t run in the background
- Cached background processes
- What about background apps?
- Summary
- Close Android Application [duplicate]
- 12 Answers 12
- How to exit an Android app programmatically?
- 30 Answers 30
- How to close Android application?
- 22 Answers 22
What is the right way to close apps in Android?
I’m new to the Android platform, and this is probably a dumb question, but.
How do you close an app?
When I’m in an app, and I want to get out, I just click on «Home» and move on, but I just installed a Task Manager app, and I noticed everything is left running.
Is there a way to exit apps? Or do I need to use the Task Manager every time I exit something?
6 Answers 6
If you exit the app using the BACK button then it will call onDestroy() on the current Activity, If you press HOME it only calls onPause()
Don’t be too worried about it though, Activities won’t drain battery they are just left in memory so they can be opened faster in the future.
I wouldn’t recommend using one of those task killer apps Android is designed to keep that stuff in memory for a reason, using a task killer is only one more extra service tugging away at your battery.. What you need to watch out for is apps that run services in the background like twitter or email clients pulling their servers, any good app that has a service like that will have an option to turn it off.
I’m pretty sure that everything I’ve ever read says that the Back button is the right way to exit out of apps.
Pressing Home leaves them running in the background until Android decides that they’re not needed any more, which can be handy if you just need to switch between apps to check something (holding Home for a couple of seconds gives you the list of recently running apps you can switch back to on most phones) but coming out using the back button tells the app that you want it to close.
Some apps have an «Exit» option in their menus. Most don’t.
You don’t really need to worry about it. You can just go to whatever app you need next. Android does a pretty good job freeing up resources when required.
Activities don’t run in the background
In Android, activities (that is, the part of the app you can see) never run in the background. They can only run (and use battery power) while they’re on the screen. The activity stops running regardless of whether you use home or back to leave it. The only difference is what data Android asks the app to save, so neither option is «the right way». It just depends on what you want to do.
If you use home, Android leaves the app in the same state, so that if you come back to it later (e.g. through the recent apps list), it’ll still be in the same state you left it: on the same screen, with the same stuff shown. For example, if it’s an email app, and you were looking at one email, then it’ll remember which email that was, and show you the same one.
Eventually (after about half an hour), Android concludes that you’re not coming back to the app, so it resets this state: next time you start the app, it’ll start from the front/main screen. To continue the example, the email app will forget which email and folder you were looking at, and show you the inbox.
If you use back, you’re telling Android that you don’t want to come back to this view. It’ll destroy the information about what you were looking at right away. Next time you start the app, it’ll show the front screen (e.g. the inbox).
As others have said, apps can control the behaviour of the back button: for example, web browsers use it to go back in the browser history. What I’ve described is the default behaviour of the back button, and developers are urged to keep the behaviour like that to avoid being confusing.
Cached background processes
Whichever method you use, Android will leave the app in memory (but not running) for as long as it can. This is to be more efficient. When you come back to the app, if it’s still in memory, Android can run it again right away; if it isn’t still in memory, then Android has to spend time and energy loading the app from storage again.
In old Android versions, apps left in memory in the background this way were included in the list of «running apps». This is a little confusing for users — it makes people think the app is really still running — so newer versions call these apps «cached background processes», to make it clear they’re only cached, not running.
What about background apps?
Earlier on, I said that activities don’t run in the background. So how does your email client check for mail? As well as activities, apps can have services. Services don’t have any GUI for you to see or interact with, but they do run in the background. Usually, a service will only run infrequently, such as to check mail once an hour, but it’s possible for the app developer to run the service all the time, draining your battery.
Leaving an activity with back or home doesn’t change how Android treats any services from the same app: the service can continue to run, or be triggered later at a given time (next time the mail check is due).
Summary
In summary, it doesn’t really matter whether you use back or home: it only changes what the app shows you next time you run it. It doesn’t have an effect on battery use. Neither of them corresponds to «exiting» a program on your PC.
Источник
Close Android Application [duplicate]
Please suggest how I may close my whole Android Application with one line code.
12 Answers 12
Yes — Why, then how(sort of):
This nicely and cleanly terminates the whole java machine which is dedicated to running the app. However, you should do it from the main activity, otherwise android may restart your app automatically. (Tested this on Android 7.0)
Details and explanation of why this is a good question and a programmer may have a very legitimate reason to terminate their app this way:
I really don’t see the gain in speaking harshly to someone who’s looking for a way to terminate their app.
Good is a friendly reminder to beginners that on Android you don’t have to worry about closing your app — but some people actually do want to terminate their app even though they know that they don’t have to — and their question of how to do so is a legitimate question with a valid answer.
Perhaps many folks live in an ideal world and don’t realize that there’s a real world where real people are trying to solve real problems.
The fact is that even with android, it is still a computer and that computer is still running code, and that it is perfectly understandable why someone may wish to truly exit their «app» (i.e. all of the activities and resources belonging to their app.)
It is true that the developers at Google designed a system where they believed nobody would ever need to exit their app. And maybe 99% of the time they are right!
But one of the amazing things about allowing millions of programmers to write code for a platform is that some of them will try to push the platform to its limits in order to do amazing things! — Including things that the Android Developers never even dreamed of!
There is another need for being able to close a program, and that is for troubleshooting purposes. That is what brought me to this thread: I’m just learning how to utilize the audio input feature to do realtime DSP.
Now don’t forget that I said the following: I well know that when I have everything done right, I won’t need to kill my app to reset the audio interface.
BUT: Remember, perfect apps don’t start out as perfect apps! They start out as just barely working apps and grow to become proper ideal apps.
So what I found was that my little audio oscilloscope test app worked great until I pressed the android Home button. When I then re-launched my oscilloscope app, there was no audio coming in anymore.
At first I would go into Settings->Applications->Manage Applications->AppName->Force Stop.
(Note that if the actual linux process is not running, the Force Stop button will be disabled. If the button is enabled, then the Linux process is still running!)
Then I could re-launch my app and it worked again.
At first, I was just using divide by zero to crash it — and that worked great. But I decided to look for a better way — which landed me here!
So here’s the ways I tried and what I found out:
Background: Android runs on Linux. Linux has processes and process IDs (PIDs) just like Windows does, only better. To see what processes are running on your android (with it connected into the USB and everything) run adb shell top and you will get an updating list of all the processes running in the linux under the android.
If you have linux on your PC as well, you can type
adb shell top | egrep -i ‘(User|PID|MyFirstApp)’ —line-buffered
to get just the results for your app named MyFirstApp. You can see how many Linux Processes are running under that name and how much of the cpu power they are consuming.
(Like the task manager / process list in Windows)
Or if you want to see just the running apps:
adb shell top | egrep -i ‘(User|PID|app_)’ —line-buffered
You can also kill any app on your device by running adb shell kill 12345 where 12345 is it’s PID number.
From what I can tell, each single-threaded app just uses a single Linux process.
So what I found out was that (of course) if I just activate the android Home option, my app continues to run. And if I use the Activity.finish() , it still leaves the process running. Divide by zero definitely terminates the linux process that is running. Killing the PID from within the app seems the nicest so far that I’ve found, at least for debugging purposes.
I initially solved my need to kill my app by adding a button that would cause a divide by zero, like this in my MainActivity.java :
Then in my layout XML file section for that button I just set the android:onClick=»exit».
Of course divide by zero is messy because it always displays the «This application stopped. » or whatever.
So then I tried the finish, like this:
And that made the app disappear from the screen but it was still running in the background.
So far, this is the best solution I’ve tried.
UPDATE: This is the same as above in that it instantly terminates the Linux process and all threads for the app:
It instantly does a nice full exit of the thread in question without telling the user that the app crashed.
All memory used by the app will be freed. (Note: Actually, you can set parameters in your manifest file to cause different threads to run in different Linux processes, so it gets more complicated then.)
At least for quick and dirty testing, if you absolutely need to know that the thread is actually fully exited, the kill process does it nicely. However, if you are running multiple threads you may have to kill each of those, probably from within each thread.
EDIT: Here is a great link to read on the topic: http://developer.android.com/guide/components/fundamentals.html It explains how each app runs in its own virtual machine, and each virtual machine runs under its own user ID.
Here’s another great link that explains how (unless specified otherwise in manifest) an app and all of its threads runs in a single Linux process: http://developer.android.com/guide/components/processes-and-threads.html
So as a general rule, an app really is a program running on the computer and the app really can be fully killed, removing all resources from memory instantly.
(By instantly I mean ASAP — not later whenever the ram is needed.)
PS: Ever wonder why you go to answer your android phone or launch your favorite app and it freezes for a second? Ever reboot because you get tired of it? That’s probably because of all the apps you ran in the last week and thought you quit but are still hanging around using memory. Phone kills them when it needs more memory, causing a delay before whatever action you wanted to do!
Update for Android 4/Gingerbread: Same thing as above applies, except even when an app exits or crashes and its whole java virtual machine process dies, it still shows up as running in the app manager, and you still have the «force close» option or whatever it is. 4.0 must have an independent list of apps it thinks is running rather than actually checking to see if an app is really even running.
Источник
How to exit an Android app programmatically?
I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this and found calling finish() will do it. But, finish is only closing the current running activity right? I have lot of activities so in this case I have to pass each and every activity’s instance and finish them or make every activity into Singleton pattern.
I also got to know Activity.moveTaskToBack(true) can get you into the home screen. OK, this is not closing but backgrounding the process. So is this is effective?
Which method should I use to close the app completely? Any of above described or any other method/other usage of above methods?
30 Answers 30
The finishAffinity method, released in API 16, closes all ongoing activities and closes the app:
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
Since API 21, you can use:
Finishes all activities in this task and removes it from the recent tasks list.
Источник
How to close Android application?
I want to close my application, so that it no longer runs in the background.
How to do that? Is this good practice on Android platform?
If I rely on the «back» button, it closes the app, but it stays in background. There is even application called «TaskKiller» just to kill those apps in the background.
22 Answers 22
Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. You can also kill an application quickly via android.os.Process.killProcess(android.os.Process.myPid()) if you prefer. The best way to do this is put a method like the following in a helper class and then call it whenever the app needs to be killed. For example in the destroy method of the root activity (assuming that the app never kills this activity):
Also Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application. However you can determine with the HOME key is pressed by setting a flag to true in a helper class that assumes that the HOME key has been pressed, then changing the flag to false when an event occurs that shows the HOME key was not pressed and then checking to see of the HOME key pressed in the onStop() method of the activity.
Don’t forget to handle the HOME key for any menus and in the activities that are started by the menus. The same goes for the SEARCH key. Below is some example classes to illustrate:
Here’s an example of a root activity that kills the application when it is destroyed:
Here’s an abstract activity that can be extended to handle the HOME key for all activities that extend it:
Here’s an example of a menu screen that handles the HOME key:
Here’s an example of a helper class that handles the HOME key across the app:
Источник