0

I launch activity B from activity A, now I want to go back to activity A rather than Launcher when I press home key, how to implement?

I found a similar case, the LockPatternKeyguardView, which launches a emergency dialer, then press home key, can back to LockPatternKeyguardView again, how to do this?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Victor S
  • 4,021
  • 15
  • 43
  • 54
  • this link may help you : http://stackoverflow.com/questions/5547818/can-i-override-the-home-button-in-my-application – Naresh J Oct 15 '12 at 08:36

6 Answers6

1

You can't override the behaviour of home button because if you do so you can make user never exit your App, which android will not let you do.

maninder singh
  • 1,286
  • 13
  • 16
0

The Home button is a very dangerous button to override and, because of that, Android will not let you override its behavior the same way you do the BACK button.

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

You can launch your activity when you press button. For that you have to declare your activity's action as HOME.

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

But there are disadvantages of this:

  1. A chooser will appear when you press home button. This will let you decide which activity to launch. As default launcher app also has same action (HOME), it will ask you which activity to launch.
  2. You can make your activity a default activity when user presses on home button. You can do it when you get chooser dialog after pressing home button, just check that check box appeared on chooser dialog.

In 2nd case, you can make your activity a default activity when you press home button but then it will not launch default Home sceen which is very usefull when you want to launch other apps. So its always advisable not to handle Home button.

Also you cannot handle Home key same as other key events(like Back Key, Menu Key etc.)

AndroDev
  • 3,236
  • 8
  • 35
  • 49
0
case R.id.home:                     
        Intent i = new Intent(this,Aactivity.class);
        startActivity(i);
        return  true;    

i knew only one way that i present....

0

Pressing the home button means the user wants go out of your app and you should not override it to retain the user. Also, only the Home button is able to get the user out of a stuck app. Consider having a menu item as 'Home' or override the back button.

ThePCWizard
  • 3,338
  • 2
  • 21
  • 32
0

when home button is pressed,activity goes to onStop() state.So override onStop() method in activity B and finish the activity.like below,

 protected void onStop() {
        finish();
        super.onStop();
    }
jagan g
  • 31
  • 8