0

I'm new android developer, my app closes when I press back from any page in menu. I added this code with dialog but it is not working

@Override
public void onBackPressed() {
    super.onBackPressed();

    FragmentManager fm = getSupportFragmentManager();
    int count = fm.getBackStackEntryCount();

    if(count == 0) {
        // Do you want to close app?
    }

}

ndnenkov
  • 35,425
  • 9
  • 72
  • 104

3 Answers3

1

Have you tried putting the super call in an else block so it is only called if the key is not KEYCODE_BACK ?

/* Prevent app from being killed on back */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

That should work for now! Feel free to comment if you have any problems.

user3648217
  • 35
  • 1
  • 7
0

try this:

@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    int count = fm.getBackStackEntryCount();
    if(count == 0) {
       // Do you want to close app?
       showDialog();
    }
}

public void showDialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Do you want to close the app");
    alert.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
              finish();  //or super.onBackPressed();
        }
    });
    alert.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert.show();
}
Aditya Chauhan
  • 271
  • 3
  • 13
0

Override activity's onBackPressed with the following in case you have for instance made changes in some values and forgot to update those afterwards, but in stead pressed the back button :

@Override
  public void onBackPressed() {
    if( <condition>) {
      AlertDialog.Builder ad = new AlertDialog.Builder( this);
      ad.setTitle("Changes were made. Do you want to exit without update?");
      ad.setPositiveButton(
              "OK",
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  backPress();
                }
              }
      );
      ad.setNegativeButton(
              "Update the changes",
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  <Update the changes>;
                  Toast.makeText(getApplicationContext(), "Changes were updated", Toast.LENGTH_SHORT).show();
                  backPress();
                }
              }
      );
      ad.setCancelable( false);
      ad.show();
    } else {
      backPress();
    }
  }

  private void backPress() {
    super.onBackPressed();
  }
Kees
  • 21
  • 2