views:

80

answers:

2

We all know that hitting the back button on the device doesn't kill the application, it merely finishes (destroys) the activity running on the foreground.

Well I have come across some code which helps me capture the back button signal so that I cannot exit the application. The only way to exit it in such cases is to press the home key.

Now this situation presents me with a unique disadvantage! The inability to kill finish the application on a time of my own choosing allows the application to keep running in the background like nothing has changed.

So in such cases is the task manager my only friend or is there a way for me to otherwise kill this application?

PS: if there is a flaw in my understanding of what happens when the home button is pressed, I would love to be corrected... =)

+3  A: 

If you want to finish your application every time when a user clicks home and relaunches your application you can specify this property in your Manifest android:finishOnTaskLaunch="true" . However Android does not permit us to kill an application through code and is not recommend.

Rahul
Actually android does permit us to kill us through code. Its called killBackgroundProcess(). Look it up. It pretty much kills the process running in the kernel level. You can verify by running adb shell and top. Thanks for your response..
Shouvik
First of all that particular method supports API Level 8 (which means SDK 2.2) .Hence you wont be able to do the same in devices running lower SDK version. As of now devices running 2.2 SDK is only 4.5%(As per google) Though i haven't used it i am not sure if it would kill your entire application as such or would kill/restart your background processes.http://groups.google.com/group/android-developers/browse_thread/thread/6a544bfd05bf10cfhttp://stackoverflow.com/questions/2877140/android-2-2-deprecates-restartpackage-but-adds-another-headache
Rahul
+3  A: 

There is no way to kill an application with the standard buttons neither the back button or the home button. Your application will be paused if it is not a service and is send to the background, and will look like it is eating up memory but if the application has no tasks running the app is perfectly fine and will not use any resources that are needed by other processes.

As a developer just be sure that you don't change the behaviour of the back button and that you unregister all listener in the onPause method of your activities. This will stop your app from consuming battery and processing power if it is in the background. If you need a background process start it as a service and give the user the possibility to disable it.

This life cycle behaviour is different than that you are used to from a normal machine. But if the apps are written more or less correct it should fit much better to the usage of a mobile device. You should read at least the multi threading the android way on the android blog maybe also the When to include an exit button. That will give you a better understanding of the intended usability patterns on android regarding multi touch and exiting applications.

Janusz
Thanks for your response. I did not mean I want to kill the application. More like intend the finish procedure to be called. But then when I change the purpose of the back button then in such cases the finish will not be called when the home button is called. So I think I got what I need from your code to understand how the application will not be hogging resources even if the back button is disabled! thanks...
Shouvik