In made the android app. I installed it in my mobile. When I press back button on my mobile it comes out of the app. But it is running in the background. So, how can I finish the activity completely? I wrote the code onkeydown event also. Even it is running in background.
You should override the onStop() method on your activity and in call the finish() method from it. Don't forget to call super.onStop() at the beginning of the onStop() method.
As far as I know it's not running on the background, android saves the state of the activity and when it comes back, it restores your activity with the same state it was.
You can call Activity.finish() to kill of an activity completely.
Be aware that this it not something your should normally do, as Android really want to manage the activity life cycle for you, as mentioned by Sheikh Aman.
Hi see the following link in that also same question is available.Finish the android application completely
I will keep running in the background. Android decides when to kill your activity.
By the way, How do you ensure that it is running and "working" (is not frozen) ??
In your onKeyDown() handler you should add this:
finish();
This causes the activity to be closed.
Call this:
android.os.Process.killProcess(android.os.Process.myPid());
It's odd, but only this call guarantees that you will kill your application. Due to specific Android life-cycling neither finish() nor any other methods doesn't kill application.
Related
I'm working on an app that targets Api 19, and that is basically a Processing sketch.
The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours.
This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from Eclipse either.
Summarising, this is what happens after the first force close (and what I deem correct):
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onResume()
And this is what happens -ONLY- the first time the app is run after installation:
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onCreate() <-- !! note no onDestroy()
onResume()
I wonder if the fact that I'm using Immersive Mode has anything to do with this, but changing the Api target version to 10, removing Immersive mode or testing on old devices do not help. I have, of course, used android:configChanges="orientation|keyboardHidden|screenSize" on my manifest.
Does anyone have a clue on what might be causing this? Is this a common issue or should I look for a bug in my code? Maybe a Processing bug?
Thanks in advance for any clue. I hope this is the right way to ask about this issue. This is my first post.
Update:
My explanation is not very accurate, but apparently there is a bug report for this. The problem is much better explained here: https://code.google.com/p/android/issues/detail?id=26658
Unfortunately, I can't get the proposed solutions to work, using this in onCreate() causes my app to either close or crash:
if (!isTaskRoot()) {
finish();
return;
}
Ok, this is how I solved it, in case anyone else bumps into this wall.
This might affect especially people coming from the Processing Developing Environment, as it converts a "Processing sketch" into the only activity of the project.
The original problem (Android managing apps in a different -wrong?- way when launching them from the Package Installer) is well explained here: https://code.google.com/p/android/issues/detail?id=26658
Solutions posted there might solve most cases,but if -like me- your launcher activity is the one that carries out all the work, you will need to create a specific launcher activity that just starts the main one... and commits suicide when the Android bug happens.
Add this bit to the onCreate() method of the launcher activity:
if (!isTaskRoot()) {
finish();
return;
}
I hope this helps.
This looks like a valid application lifecycle, you put your app to background, android is then allowed to destroy your app. onDestroy is not guaranteed to be called, you have to be prepared for that, as you say onPause is called so you can use it. Why it happens only once after install is hard to explain, but in my opinion you should not actually care about it and prepare your app to be killed any time it is in background.
In above diagram may OnDestroy method never called because other application need memory.
onStop() is the last method which is guarantied to be called. After this method Android is allowed to kill your activity at any time. Check out the table in the activity's lifecycle table. There is a "Killable" column describing the time, when activity can be killed.
If you don't use static variables and you initialize everything properly in onCreate(), then you should have no issues with this behavior. Just never expect onDestroy() to be called.
I can't figure out the onDestroy() behaviour.
My question is: Is there any chance that an activity will be killed without calling it's onDestroy() while not killing the hole app?
I mean, Could it be that I'll get back to my app (to an activity other then the activity that the launcher calls) and be in a situation where one activity was killed without calling it's onDestroy()?
I have a need to know that if I get back from the background to an activity that there is no way some of my activities where killed without it's onDestroy.
Thanks!
No i don't think so , when your application get killed because of lack of Memory your whole app process would be killed so in this situation onDestroy() may not be called and your app will back again on your launcher Activity unless you can save your application state on onPause() state before your app get killed.
yes, Android will kill a least frequently used activity if there is not enough memory is available for the newly started app. Also the back button triggers the onDestroy(). A best bet is to save your app state. Here is an example to a similar question how to save and restore your current instance.
As stated in the API documentation Activity#onDestroy():
Note: do not count on this method being called as a place for saving
data!
http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
And don't forget to call super.onDestroy()
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.
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.
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