How I can change working of home button in android? I want to when I click on home button I do some actions and after that application should go to background. How I can do that.
Write your own home screen. When the user presses HOME, they will get a choice of running your app or the built-in home screen. They can elect to choose one just this one time, or set either app as being their default from now on. Many will wonder why on Earth you decided to decided to implement a home screen.
Most developers care about any case where their activity moves to the background, in which case you can either use a lifecycle method (e.g., onPause(), onStop()) or try onUserLeaveHint().
I do it this way:
/* Handles item selections */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//You can do whatever you want here
Intent homeInt = new Intent(this, SomeActivity.class);
startActivity(homeInt);
return true;
}
return false;
}
Have you tried overriding onKeyDown()
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// ENTER CODE HERE
return true;
}
return super.onKeyDown(keyCode, event);
}
Related
I've got a Samsung tablet SM-T560 on which when pressing the touch 'menu' key (next to the hardware 'home' button) opens the system menu for 'Active apps' where you can close or navigate to a previously opened app.
How can i override this functionality?I want to disable showing active apps upon this menu button click.
you need to override the menu button using the onKeyDown(int, KeyEvent) method in your activity. The following code snippet should be a good start for you:
#Override
public boolean onKeyDown(int keycode, KeyEvent event) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
//Your functionality here
return true;
}
return super.onKeyDown(keycode, event);
}
When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}
My requirement is that when user clicks the home button at any screen in application, he is redirected to device home screen and when he comes back to the application he will redirect to app home screen rather than at screen where he pressed the home button.
Any help would be appreciaed.
As you said that you want to go Home Screen when you press Home button from any Screen. So you better take a BaseActivity extending Activity and inside that override onKeyDown() as below.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
// Implement starting Home Activity with Clear Top
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Then Extend all your activities with BaseActivity instead of Activity. So that Every Activity get onKeyDown() Functionality so that any where you press Home Button it navigates back to HomeScreen with removing all Activities.
Here is some code you can use to detect Home-Button pushes and call appropriate functions.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or
In android manifest do setting android:noHistory="true" on the activity,this will remove an activity from the stack whenever it is not there in foreground.
I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").
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);
}