Android Listen Home button
Is it possible to listen home button uniquely, if the application contain some default intent? I have checked life cycle method but it will execute when I a开发者_如何学Gom starting the default intent and home button.
An acticity can have more than one intent filter. so in the manifest add another intent filter like this to listen fro home button.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
try this let me know
when the user press the home button and resuming the operation i need to perform dbsycing – Sudeep SR Dec 6 '10 at 12:45
It sounds like you want to override the onStop or onStart methods of your activity. There are things other than the home button that can cause your application to exit (sent to background). The back button, camera button, incoming phone call, selecting an item in the pulldown notification bar. I don't think you want to rely on just the home button exit case. Here's the activity lifecycle link that is always good to reference in these situations: http://developer.android.com/guide/topics/fundamentals.html#actlife
You can do it with a global static boolean flag that will tell you if exiting the Activity was intended. You need to raise that flag in any "finishing" procedure. (exit procedures / back key pressed). Here is an example:
/**
* Home Event
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
StaticClass.exitFlag = true;
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onUserLeaveHint() { // not executed when answering a call
if (StaticClass.exitFlag)
StaticClass.exitFlag = false;
else
// Home was pressed!!!
super.onUserLeaveHint();
}
/**
* End Home Event
*/
This way you control when and how you exit the activity. There are only two ways for the activity to disappear without your knowledge and thats by answering a call or by pressing Home. This example eliminates the option for answering a call with the onUserLeaveHint() event!
Good luck!
精彩评论