How to Detect User Pressing Home Key in My Activity

overriding the Home Key Long press in a category.HOME activity

Everything I have ever read states that this can't be done... Here is a post on Android Beginners where I asked a very similar question:

http://groups.google.com/group/android-beginners/browse_thread/thread/d8cdcd1c52d79ef1/0f4b184da6f248a9?lnk=gst&q=home+key#0f4b184da6f248a9

However, I have recently come across an app that successfully allows you to launch it by double-tapping the home key so there has got to be something that can be done. I looked into that approach for a while but couldn't get it to work. Now that I know someone else figured it out I'm going to take another stab at it....

EDIT
While overriding a long-press of the home button cannot be done, I have found a way to successfully implement a double-press of the home button. The general idea for this is as follows:

  1. Make your app act as a home replacement app (Look at the sample home app in the SDK samples)
  2. Allow a way in your app to specify a home app to use (it is pretty straightforward to present the user a list of home-replacement apps)
  3. On the first press of the home button start a timer.
  4. If the timer times out, launch the home application
  5. If the user presses the home key a second time before the timer stops, launch your app

Essentially, the home-replacement activity does nothing more than either launch the real home app specified by the user or launch your app... It never displays its own UI.

I have found that this works pretty well, and actually have an app published in the Android Market that does this. If you would like to see it in action, it is called "Quick Launch" and the publisher name is listed as "MagouyaWare"

Hope this helps!

Check if back key was pressed in android?

A couple of ideas:

  • You can just set a flag in MainActivity when it fires up NewActivity.
  • You can call startActivityForResult from MainActivity and arrange for NewActivity to set a result, which you will receive in MainActivity.onActivityResult() when NewActivity finishes.

Detect when home button is pressed iOS

These are your options

In your app delegate:

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Kill all activities when HOME key is pressed android

Set android:clearTaskOnlaunch="true" on the activity launched from the home screen.

You might also check some of the other attributes you can specify on activity, to tweak it's behavior a bit more.

How to check if the back button was clicked

This is how I understand your problem:

NfcSettingActivity only shows when a certain setting (probably NFC) is not activated. If not set, the user can click your button in the activity and will take the user to the NFC Setting of Android. Once activated, the user clicks back button and takes him/her back to the NfcSettingActivity. At this point, you want to know if the NFC setting was successfully activated.

Here is what you need to do: You don't need to catch back button press. What you need to do is check if the NfcSettingActivity is in resumed state again. In NfcSettingActivity, you need to have the following:

@Override
protected void onResume() {
super.onResume();

// Do your NFC checking here!
// Also, you might need to add a flag here to check if the user has been to the NFC Setting page already!
}

Understand the Activity Lifecycle

What methods are invoked in the Activity Lifecycle in the following cases:

both pressing home button and receiving a call don't remove the activity from the task's stack, and will be available when you re-enter the app => onPause() => onStop().

as the activity lifecycle diagram shows, re-entering the app calls => onRestart() => onStart() => onResume()

pressing the back button instead kills the activity => onPause() => onStop() => onDestroy()

re-entering the app in this case calls the classics => onCreate() => onStart() => onResume()

EDIT

from http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

If an activity has lost focus but is
still visible (that is, a new
non-full-sized or transparent activity
has focus on top of your activity), it
is paused. A paused activity is
completely alive (it maintains all
state and member information and
remains attached to the window
manager), but can be killed by the
system in extreme low memory
situations.



Related Topics



Leave a reply



Submit