Overriding the functionality of Home Button

时光怂恿深爱的人放手 提交于 2019-11-29 16:14:42

Try this:

@Override
    protected void onUserLeaveHint() {
        if (!navigating) {
            Intent intent2 = new Intent();
            intent2.setClass(ActivityA.this, ActivityB.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            forceHome(this, intent2);
        }
        super.onUserLeaveHint();
    }

public static void forceHome(Context paramContext, Intent paramIntent) {

        if (paramIntent != null) {
            ((AlarmManager) paramContext.getSystemService(ALARM)).set(1,
                    System.currentTimeMillis(),
                    PendingIntent.getActivity(paramContext, 0, paramIntent, 0));
        }

    }

As a security feature of android, the activity only launches after 5 seconds. If you want to launch it immediately. use your own Home Launcher.

No you can't override the functionality of HomeButton. Its for security reasons so that malicious apps does not take control of your home button.

If you want to handle home button inplement home screen.

I am not sure if the below is helpfull.

You can Override onUserLeaveHint

protected void onUserLeaveHint () if you want to

Added in API level 3Called 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.

Try to override this method and write your code.

 @Override
 protected void onStop() 
 {
     super.onStop();
     startActivity(new Intent(this, ActivityB.class));
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!