How to kill an An Android Application Programatically - android

I am working on Android applications and getting one problem. I am using Exit Button, so when I am clicking the exit button (which is present in last activity of the application or in Middle but not in First Activity) then the application should close, and when I am restarting the application it should start from the first screen.
But my problem is it's not killing the application and when I am starting again it is starting from the previously paused activities.
I have used System.exit(0) and also used android.os.Process.killProcess(android.os.Process.myPid()).
but those are not working.

in an Activity: this.finish()
in a Service: this.stopSelf()
in a Receiver: return;

You should not. It goes against basic android ideas. Please read about applciation lifecycle.
You could finish individual activity / service with finish()

System.exit(0) will do the job. However, it is not at all recommended. Android does some caching, and saving state, so as to relaunch your appp quicker, the next time you launch it.. so you do not want to be messing around with that.
This has been discussed many times, please research your question before posting.

Related

how to force android system to recycle all background activities?

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();

How to force close my Android application?

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.

Android splash Activity only shows up once

I have created a slash screen for my Android app following this tutorial. Basically it's just start a Thread in the onCreate() of the splash Activity, and wait for a short period of time before switch to the main activity.
This works fine ... only on the first time running. The splash screen only shows up once after installation or rebooting my phone. On the second time, the app just skips the splash Activity like it doesn't exists.
I suspect it has something to do with Android Activity life cycle, maybe the app doesn't really exist and stays in memory. I tried killing it by: 1) Using the "Advanced Task Manager" app, and 2) Programmatically killing it in onDestory(), but none of those worked. The splash screen still won't show up after the first time.
Can anyone help me with this? Thanks a lot,
Instead of putting it in your onCreate() you could try to put it in the onStart() method. That way it'll show for each launch not just each creation which might get around your lifecycle issue.

How can I programmatically close an application?

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.

Destroy Android application

I have developed an application which is working fine.
In that i have used some static variables and also set Application level variables.
My problem is that even after setting finish() on each activity, application is showing in Running mode.
After closing the application, when i start the application after sometime, it will set the last changes.
How can i destroy my application?
I think the correct answer is "you shouldn't".
It doesn't matter if your app is still running after the user stops using it. Android will kill it when/if it needs to.
Your app most likely shouldn't even have an exit button.
I have checked that application does not release the resources.
So, what i have done is :
Process.killProcess(Process.myPid());
It will kill the application.
It helps me a lot.
call
System.exit(0);
from within
OnDestroy()
And yes, you shouldn't.
I hope you have called onDestroy for your activity.
Secondly, are you saving your preference some where else??
Please go through these methods used for the destroying the activities
setResult() finish() and onActivityresult()
After calling setResult() call finish() for the activity that you want to finish & then in it's parent activity override onActivityResult() in this you can finished your application.
The solution given by oli is right android will destroy your application running. But I want add, only if it is lacking in system resources.
For more help please go through following link.
http://developer.android.com/reference/android/app/Activity.html

Categories

Resources