Search code examples
androidandroid-actionbarback-button

android default Back button minimizes the application after I implement code for action bar back button


I have implemented code for action bar button and it works fine but after that my smart phone back button is closes or minimizes when I press it. Here is my Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);

    /**
     * Action Bar Back Button
     */
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
         // Respond to the action bar's Up/Home button
         case android.R.id.home:
         NavUtils.navigateUpFromSameTask(this);
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
   @Override
   public void onBackPressed() {
      moveTaskToBack(true); 
      AboutActivity.this.finish();
   }

Solution

  • public void onBackPressed() {
      moveTaskToBack(true); 
      AboutActivity.this.finish();// you are finishing the activity
    }
    

    if you want to go back try this

    @Override
    public void onBackPressed()
    {
         // code here to show dialog
         super.onBackPressed();  // optional depending on your needs
    }