how to emulate home button on back button pressed - android

I'm trying to make my app not close when the user pressed the back button and remain in the same state at least for a while, like the home button. I searched and some people suggested to do this.
#Override
public void onBackPressed() {
super.onBackPressed();
this.moveTaskToBack(true);
}
but this didn't help. I have to mention the activity I'm using this code on is not the LAUNCHER activity and before this activity, there is a splash screen.
what am I doing wrong?

Try my code in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown: ");
if(keyCode == KeyEvent.KEYCODE_BACK){
goHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void goHome() {
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
Do start the home screen when key back down.

You should not call super method in onBackPressed(). It should be simply like this
#Override
public void onBackPressed() {
moveTaskToBack(true);
}

Related

How can I disable click event on Home key in android?

I am Using following method to disable HomeKey click event ,but after Clicking Home key My App will be closed. I want ,whenever user click home key my app will not be close.
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
moveTaskToBack(false);
}
Since Android 4 there is no effective method to deactivate the home
button. That is the reason why we need another little hack. In general
the idea is to detect when a new application is in foreground and
restart your activity immediately.
Check this workaround: http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
Override the below method in your Activity,
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#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;
}
Put his into your activity's code. It will intercept back button click
#Override
public void onBackPressed() {
}
u can ovverride home button pressed like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}

Android: Detect back button release

I am able to detect when the back button is pressed by using:
#Override
public void onBackPressed() {
//Do my thing
}
But now I want to detect when the back button is released. How can I do that? I'm unable to find any method like onBackReleased() which I can override to do my thing.
There is no method that specifically detects the back button release, but you could use the onKeyDown() and onKeyUp() methods. Check to make sure the key is the back button, and do your stuff in the method. Here is something you could do:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do your stuff for when the back button is initially pressed
return true;
} else {
return false;
}
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do your stuff for when the back button is released
return true;
} else {
return false;
}
}
If you wanted to override the default back press, you could just use the onBackPressed() method, like so:
#Override
public void onBackPressed() {
// Prevents the user from clicking the back button and returning to the parent activity
}
These methods can be implemented directly in your class. Hope it helps!

How can I close my application on back pressed

I want to close application when I press device's back button.I am using this code..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the onBackPressed() in your Activity where you want application to Quit when the device back button clicked
#Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This code does it :
public void onPause() {
super.onPause();
finish();
}
Don't forget to mark it as answer if it helped ;)
The answer that you marked as correct will stop the app every onPause of this activity, which is not an event of the back button cick, the right answer is :
#Override
public void onBackPressed() {
finish();
}

Glass: How to gracefully exit on a downswipe to the main menu

For a Google Glass App, how do you make you app gracefully exit on a downswipe to the main glass menu?
Trying this code in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
return;
}
}
#Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK){
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent); }
return false;
}
This doesn't seem to "exit" a Google Glass app. This may work for Android apps but this causes a Glass app to enter a weird state where a blank screen is shown permanently. I want to "exit" and see the main Google glass interface or exit the app and put the glass into it's default sleep mode.
All you need to do is call finish() on the keydown.
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK){
finish(); }
return false;
}
public void onDestroy() {
super.onDestroy();
}
You might face a problem with the onKeyDown method, use gesture detector in that case.
if (gesture == Gesture.SWIPE_DOWN) {
finish();
return true;
}
You can check here for complete reference.

Back button works like home

Is it possible to change this code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event);
}
so that when I click on back button it works like a home button?
try this,
#Override
public void onBackPressed() {
Intent backtoHome = new Intent(Intent.ACTION_MAIN);
backtoHome.addCategory(Intent.CATEGORY_HOME);
backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(backtoHome);
}
Adding this to your Activity, will make it look like your app is responding to a Home Button Click event
One line solution
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
And also see this
#Override
public void onBackPressed()
//super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
If something doesn't work like expected try it with uncommented
super.onBackPressed();

Categories

Resources