1

I need to override Home button for Lock Screen App. And I found the following answer which restart the app after 6 seconds on home button press.

protected void onUserLeaveHint() {
    Intent i=new Intent(this,MainActivity123.class);        
    startActivity(i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
    Toast.makeText(this,"leaveHint",Toast.LENGTH_SHORT).show();
}

But I want to start the app immediately. So I guess thier was an answer saying if we make our app default Launcher app than thier will be no time gap.So i added in my manifest

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

But this is not working and app still restart after 6 sec. what should i do.

So Spend some more researching and found another method {used to go to a default screen in launcher When you are already on home screen} The method called automatically using appropriate flag in startActivity()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Toast.makeText(this,"onNewEvent",Toast.LENGTH_SHORT).show();
    if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
            Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
        Toast.makeText(this,"onNewEvent",Toast.LENGTH_SHORT).show();
        //++goHome++
        startActivity(intent);
    }
}

The method is get called But app still starts after 6 sec. SO no progress Any suggestion........

Brahm Datt
  • 11
  • 3

1 Answers1

0

Try overriding the home button by implementing this method:

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

:) Hope it helps.

  • 1
    i have tried this onKeyDown method it does override other hard button but not the home button.Probably from API14 and above it does'nt even recognise home key. Any else suggestion Please – Brahm Datt May 06 '15 at 08:28