I am developing an android application. There is first login page and after that pages as per requirement. when i pressed button home button it comes out on android main screen. and when i starts application again it moves on Activity it left previously instead of moving on login page. please help me out. how can i achieve this?
Asked
Active
Viewed 420 times
0
-
You shouldn't do this, the Home button is a special one and Android won't let you change it's behavior. Think of another solution. – Egor Jul 13 '12 at 06:58
-
Check this, http://stackoverflow.com/questions/3898876/how-to-disable-the-home-key/8889913#8889913 – Andro Selva Jul 13 '12 at 07:00
-
So what do want to achieve?? in your question it is written what happening only – AAnkit Jul 13 '12 at 07:01
-
i want that when i press home out it comes out . and then starts application it should move on login screen instead of Activity onwhich it was. – Nitesh Kabra Jul 13 '12 at 07:08
-
Thanks all for there valuable suggestion. – Nitesh Kabra Jul 13 '12 at 07:08
2 Answers
1
See, for security reasons android developers itself are not allowing us to change any kind of behaviour wiht home button. But if you really want to disable the home button press you can do this by adding the below code from this answer:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}

Community
- 1
- 1

Daud Arfin
- 2,499
- 1
- 18
- 37
-
1I added attribution for where you drew the above code from. In the future, please do give credit to the sources that you draw your answers from. – Brad Larson Jul 13 '12 at 16:24
0
Try to override onUserLeaveHint to catch event:
onUserLeaveHint() Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.
(http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint())

Alex Kucherenko
- 20,168
- 2
- 26
- 33