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;
}
Related
Let's say I have an activity A that overrides the back button to show some dialog and that this activity has a menu.
So, when the back button is pressed the dialog shows up, but if the user press the menu button and then the back button, the dialog isn't shown. How can I make the behavior for the back button be the same whether the menu is visible or not?
You need to override the BackButton.
onBackPressed()
{
closeOptionsMenu(); // to close the Options Menu if it is visible
//your code here
}
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return true;
}
hope this help
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().
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);
}