Well apparently my android application doesnt close when I finish the activity so it there a way to force close it including any activity in my app that could be opened?
The fact that the process is still alive does not mean that the Activities did not finish. The process might be kept for a while until the OS decides to kill it.
If you have activities that do not finish properly, make sure that you did not leave a thread running.
There is no method in the API to close an Application. It is up to the OS to terminate it when it is convenient. The last resource is to kill the process, but you should never need to use that in your apps.
Make sure that threads that you have started terminate, and then invoke finish
Btw. How have you verified that you activity isn't closing?
Call finish() at the point when you want it to close. For example in onPause() if you want it to finish when the activity is no longer the topmost one.
I personally suggest you simply make sure your app does not do anything when it is not active and let the system worry about 'finishing' it.
Related
Is there API or commands to force Android system to recycle all background activities no matter there is enough resource or not? And how to check all the activities' status to check that the activity is actually killed?
There is an API called killBackgroundProcesses(), but this API is killed the whole process, I am wondering how to only kill some activities without killing the whole process.
As the android dev guide page says below, I am looking for the first way.
activity lifecycle
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process.
Yes, there is a way:
Settings -> Developer options -> Apps -> Don't keep activities
Check that box, and you are good to go.
Cheers.
I don't think so Android gives you the information of Activities are in background (i.e in Paused or Stopped State). Even if Activity Stack as well will give you the access of activity at Top.
Possible Solutions :
1. If at all you want to destroy the service , better to call finish() after startActivity() method.
2. If you want to periodically destroy all the background activities. You should implement your own activity stack. Which does pushToStack() on start of new activity and popFromStack() and then activity.finish();
My application will launch 3rd party map activity and set the place. Once user press back from Map activity, it will return to previous activity.
Once i quit the application, the map activity and other process will be keep running. i would like to close them. How should i do?
Thanks in advance
Try Launching the foreign activity with FLAG_ACTIVITY_NO_HISTORY
A process is not the same as an Activity, and it's not your concern if a process (even your own) remains alive when not in use, as Android will dispose of it when appropriate.
You should never try to kill foreign processes (you can't anymore anyway), and the situations where you want to kill or end your own are rare (mostly for testing to make sure you can recover).
My application launches 3 activities.
When I want to exit, I close these 3 activities calling finish method. The activity onDestroy methods are then called but the process is still alive...
How is it possible ?
This is perfectly normal. Android will keep your process around until such time as it needs to reclaim that process' memory. That way, if the user immediately returns to your application, your application will appear quicker -- Android does not have to fork a process and load your application into memory.
Try:
System.exit(0);
It will kill your Activity-process.
Edit:
As mentioned in the comments below. This works much better:
android.os.Process.killProcess(android.os.Process.myPid());
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?
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 :)).
Maybe this link will help developer page
I quoted the part below that i think might help you.
Shutting down components
A content provider is active only
while it's responding to a request
from a ContentResolver. And a
broadcast receiver is active only
while it's responding to a broadcast
message. So there's no need to
explicitly shut down these components.
Activities, on the other hand, provide
the user interface. They're in a
long-running conversation with the
user and may remain active, even when
idle, as long as the conversation
continues. Similarly, services may
also remain running for a long time.
So Android has methods to shut down
activities and services in an orderly
way:
An activity can be shut down by calling its finish() method. One
activity can shut down another
activity (one it started with
startActivityForResult()) by calling
finishActivity().
A service can be stopped by calling its stopSelf() method, or by
calling Context.stopService().
Components might also be shut down by
the system when they are no longer
being used or when Android must
reclaim memory for more active
components. A later section, Component
Lifecycles, discusses this possibility
and its ramifications in more detail.
You can close all activities from background and when re-open the app It starts from first activity
this.finish();
Intent intent = new Intent(getApplicationContext(), CloseApp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You can close all activities from background and when re-open the app It starts from paused activity[where you closed] activity
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Android doesn't like you closing your apps.
Here's a discussion on that:
Is quitting an application frowned upon?
If you really want to do it, for whatever reason, you need to close all your activities.
Here's a discussion on how you could do it:
Closing several android activities simultaneously
have in mind that:
finish();
method closes current Activity only.
If you have multiple Activities opened, you should call finish() per Activity.
Note: Closing Service is different.
Some Android application restarts automatically when i kill process manually.
How this can be possible?
Is there anyone who know how to do like this on Android platform?
Are you sure these applications are restarted automatically?
I suspect that they actually have a few activities on top of each other. When you kill a process in Android, only the top activity will be destroyed, other activities are still on the activity stack. In other words, when you return from the "process killer activity" another of the application's activity becomes visible, and the process is restarted.
This is not something that you need to implement in your application, it is just how Android works.