0

I am making an app where privacy is of utmost importance. So, there is a password to enter the app. Now, the problem lies when the user presses the HOME button - then, what happens is that the current task is taken to the background and the android home screen is displayed.

But when the user clicks on the app's icon on the android device, the app returns to the same private state without asking for the password.

I want to make the app always start at the Login Activity only when the app's icon is pressed.

In each and every one of my activities, I have the following code:

if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();                     
            return true;
        }
        return super.onKeyDown(keyCode, event);

But, the toast is never getting displayed...similarly, I tried to

finish();

the process in the code. But it doesn't happen. Is it not possible to control what the home button does, or am i missing something out?

Thanks!

Randomly Named User
  • 1,889
  • 7
  • 27
  • 47

4 Answers4

1

It is not possible.

There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.

If you want to handle the HOME button, implement a home screen.

But i would suggest you to rethink your design. Leave the option of logout to the user.

http://developer.android.com/training/design-navigation/index.html

Look at the gmail app. When you press home button it does not logout.

@Override 
protected void onUserLeaveHint()  
{ 
        super.onUserLeaveHint();
        Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
        Log.i("Home button Pressed!", "Yes");
        finish(); 
 }

You can use onUserLeaveHint

protected void onUserLeaveHint ()

Added in API level 3

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.

This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try to use following code.

@Override
protected void onUserLeaveHint() 
{ 
    super.onUserLeaveHint();
    Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

In every intent you can add flag like

intent.addFlag(Intent.FLAG_NO_HISTORY);

Which finishes the pages once it goes to the background.

Naresh
  • 3,174
  • 1
  • 17
  • 22
0

The home button takes the control from your application and navigate the user to home screen.. Moreover, the toast that your are displaying belongs to your application's context.. So, you can't control it.

Abhishek Shukla
  • 1,242
  • 8
  • 11