In my application, I would like to open login screen if user click home button but also want to maintain activity stack.
Is there any way to do this?
There is no direct way to do the same. like you can not get the KeyDown event for the HOME key. but we can get the HOME key captured and stay in the same screen by following code
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Actually you should override to home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent new(this, login);
start(intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
You cannot capture Home button click event definitely for security reasons.
However, you can do something along with actions taken by pressing Home button by overriding method onNewIntent().
Related
When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
I have a non-application window (i.e. a window with no activity context, that appears over other apps). This window is technically a View which has been added using WindowManager.addView() and TYPE_SYSTEM_ERROR.
I would like to remove this window when the Home button is pressed but it seems impossible to intercept the Home button press.
Is there any way to do this?
EDIT: My app does not have an Activity so cannot use any methods on the Activity class.
You can detect when a user press Home button with:
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
but you can't override system behaviour for that action.
Below method used for enabling Home button key press event
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
//Leave Blank no code like back press method so by pressng home button it will not go to home
//This can be used for long press home button also
}
return true;
}
I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity
In My android application, currently one of my activity is focused on the screen. If the user clicked on "HOME" button, which activity method gets invoked.
You cannot override anything about the Home activity. You can only use the Home intent category.
See this other question for more info:
Can I override the 'Home' button in my application?
There is no way to handle "Home" button click. And this is by design.
Read great post by CommonsWare: Please Ignore the HOME Button. Also see this and this threads on Android Developers group.
Works for me..
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Then
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME){
// Your code here.
return true;
}
return false;
}
How do I have an app return to the 'Home' screen when the user is on a different class and they click the back/return button on their phone?
Right now, when they access a different class and hit the back button, it goes back to the Home screen on the Droid rather than the Home screen on the app - how do I get it to go back to the Home Screen on the app?
Thanks!
Use OnBackPressed() method of Activity class.
Make conditions like :
public void onBackPressed(){
if(conditon)
{
detailInfoList.setVisibility(View.VISIBLE);//or anything that you want to show
}
}
you can't,
the home button is the only you can't prevent form closing your app. Its done so that you can't block the user inside your applications (like a virus).
the other keys like back menu and research are possible
in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(keyCode == 82){
Log.i("jason","released on menu");
//change view
//return so that the other behavior (leaving the activity) is not tirggered
return false;
}
}
Log.i("jason","actual key code "+keyCode);
return super.onKeyDown(keyCode, event);
}