Android: Override back button to act like home button?

Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading. ) If that's the case, then your Activity can finish as usual and the Service will still be running A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows: Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent. KEYCODE_BACK) { moveTaskToBack(true); return true; } return super.

OnKeyDown(keyCode, event); } I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. Reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion NOTE : as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading. ) If that's the case, then your Activity can finish as usual and the Service will still be running.

A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent. KEYCODE_BACK) { moveTaskToBack(true); return true; } return super. OnKeyDown(keyCode, event); } I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. Reading the current state from a Service if needed.

But moveTaskToBack can be used as a quick alternative on occasion. NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

MoveTaskToBack() appears to work as desired. What might the downsides be to using this method? Are there cases where this might not work as expected?

– bdls Jan 4 '10 at 16:14 After reading its doc more carefully, I actually think it should work fine. (I initially thought it applied to the activity, but in fact it says the "task containing this activity". ) But you should test it for yourself of course.

:) – Mirko N. Jan 4 '10 at 16:28 Thanks Mirko, the method appears to work well. It might be cool if you give more prominence to your edit at the bottom of your answer for those looking at this question later - as that was the bit I was looking for!

– bdls Jan 4 '10 at 17:20 1 Please read android-developers.blogspot. Com/2009/12/… for the recommended way to handle the back key. – hackbod Jan 4 '10 at 17:57 1 @bdls in theory your background activity could be killed by the system if it runs low on resources, so to be safe it should be able to recreate itself anyway.

I had a look at the source code for the Android Music app and don't see any special back button handling. – Mirko N. Jan 4 '10 at 18:53.

If you want to catch the Back Button have a look at this post on the Android Developer Blog. It covers the easier way to do this in Android 2.0 and the best way to do this for an application that runs on 1. X and 2.0.

However, if your Activity is Stopped it still may be killed depending on memory availability on the device. If you want a process to run with no UI you should create a Service. The documentation says the following about Services: A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time.

For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. These seems appropriate for your requirements.

Try to override void onBackPressed() defined in android.app. Activity class.

Since android 2.0 this is the correct thing to override. Developer.android. Com/sdk/android-2.0.

Html – jay Sep 29 at 19:54.

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A.

B will not be created until A's onPause() returns, so be sure toenter code here not do anything lengthy here. This callback is mostly used for saving any persistent state the activity is editing and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions