Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to minimize the android application on back button click

People also ask

How do I close an app on back press?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:

 public void minimizeApp() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

This is a simple code to minimize the application

@Override
public void onBackPressed() {
        this.moveTaskToBack(true);
}

try this code, this will minimize Activity.

public boolean onKeyDown(int keyCode, KeyEvent event)  
{
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {
        this.moveTaskToBack(true);
        return true;
     }
    return super.onKeyDown(keyCode, event);
}

or

If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);