In my application i want exit from app when press back button, this my code:
@Override public void onBackPressed() { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).setNegativeButton("No", null).show(); }
it's work correctly but when i exit from app it does not exit completely and show empty page with my app logo and when i again press back button exit from app, How can i fix it???
EDIT :
I use this code instead of above but my app exit completely but i want it running at background and does not exit completely , how can i do it?
@Override public void onBackPressed() { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") .setMessage("Are you sure?") .setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); } }).setNegativeButton("no", null).show(); }
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.
Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI.
When you press back and then you finish your current activity(say A), you see a blank activity with your app logo(say B), this simply means that activity B which is shown after finishing A is still in backstack, and also activity A was started from activity B, so in activity, You should start activity A with flags as
Intent launchNextActivity; launchNextActivity = new Intent(B.class, A.class); launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(launchNextActivity);
Now your activity A is top on stack with no other activities of your application on the backstack.
Now in the activity A where you want to implement onBackPressed to close the app, you may do something like this,
private Boolean exit = false; @Override public void onBackPressed() { if (exit) { finish(); // finish activity } else { Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show(); exit = true; new Handler().postDelayed(new Runnable() { @Override public void run() { exit = false; } }, 3 * 1000); } }
The Handler here handles accidental back presses, it simply shows a Toast
, and if there is another back press within 3 seconds, it closes the application.
Try this:
Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here*** startActivity(intent); finish(); System.exit(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With