I need to minimize the android application on back button click

I need to minimize the application when back button is pressed.

I use following code to catch hardware back button click event

  • BPG image format on Android
  • How to create a DataGrid display in android?
  • Android EditText.setError() yields invisible error text
  • Why do I get “Failed to bounce to type” when I turn JSON from Firebase into Java objects?
  • Android Studio 3.0 : canary 6 : android.content.res.Resources$NotFoundException inside Launching activity
  • Can Firebase Console send data payload?

  • help me with the code of minimize on back key pressed

    @Override
    public boolean onKeyDown(int keyCode, keyEvent event) {
        switch(keyCode) {
        case KeyEvent.KEYCODE_BACK;
        //minimize application
        return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    Related posts:

    Does the presenter having knowledge of the Activity / Context a bad idea in the MVP pattern?
    Is there a way to test android applications directly on phone, without the emulator?
    EditText : set number of characters programmatically
    Access string.xml to java code in Android
    Decode HTML entities in android
    How to set edittext preference summary and have it stick
  • How can I see Javascript errors in WebView in an Android app?
  • Set margins in a LinearLayout programmatically
  • Authenticating your client to Cloud Endpoints without a Google Account login
  • How to use Monitor (DDMS) tool to debug application
  • Animation before API 11
  • How to draw text with different stroke and fill colors?
  • 4 Solutions collect form web for “I need to minimize the android application on back button click”

    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);
    }
    

    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);

    This is a simple code to minimize the application

    @Override
    public void onBackPressed() {
            this.moveTaskToBack(true);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, keyEvent event) {
        switch(keyCode) {
        case KeyEvent.KEYCODE_BACK;
        //minimize application..assuming the current filename is MainActivity.java
        MainActivity.this.finish();
        return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    The above code will request the system to end the activity. If the system does not run out of resources, then when you reopen the app..it should open where you left off. ie. onResume() should kick in.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.