Destroy Android application - android

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

Related

How to force Android to trigger activity kill

I am having an issue that's closely related to
support FragmentPagerAdapter holds reference to old fragments
and ViewPager and fragments — what's the right way to store fragment's state?
Anyway, my problem is that my application crashes on this one activity when it gets recreated after the system kills it. As it's a pretty heavy activity and I'm debugging to implement fixes, I need to trigger the "Activity killed by android system".
Right now, I am doing "Open 20 other apps, and then hope that my app gets killed before reopening it".
Is there any better way?
PS: I have tried killing it manually (force killing) from app information. It doesn't work, as my application gets recreated from my home screen
Actually I found an answer...
In developers settings, all the way down, look for
App -> Do not keep activities
Tick it, then launch your activity, leave it using homescreen, launch any other app (gallery or whatever), and then when you relaunch your app, it will have been killed by the android system
Calling the finish() method should work.
If you are inside of the context of the activity then simply call finish();
If you are outside of the context. Then pass the context of the activity and call
activity.finish();

onCreate() called while Activity is stopped (but not destroyed). Only after installation

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.

How to kill an An Android Application Programatically

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.

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.

Completely finish the actitvity

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.

Categories

Resources