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;
}
Related
I have seen many solutions to this problem, but the solutions were for older versions. Is there any way to disable the system buttons Back, Home and Menu for one activity? I am programming for android 7 and above.
You can try override onKeyDown.This can intercept back button and do something you want.Also override onBackPressed() can intercept back button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
do something;
return true;
}
return false;
}
But the menu button and home button I'm not sure if still can intercept in now days.
You can try to lock the com.android.systemui package
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;
}
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().
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);
}
I am using following code to start activity when user pressing search button on the handset
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Util.startActivity(ReviewsDetail.this, KeywordSearch.class);
return false;
}else{
return super.onKeyUp(keyCode, event);
}
}
But here are few issues with it please look at the following image.
When press search button it first show google search box at the top of activity then start activity which i want to start
When click on the back button displays empty actiivty
#Override
public boolean onSearchRequested() {
// your logic here
return false; // don't go ahead and show the search box
}
The Search button and system's search request are both working the same when invoked from any activity of your app. If you want to override it you will have to override it for EVERY activity you want it to work from in the same way. Unfortunately, there is no way to override it "globally", neither a way to subclass/style/theme the default search popup. So sad, google.