it's not recommended to exit application directly, so the point is to finish all activities so everything goes to the background and user returns to the home screen. My problem is that I have main activity, which is always launched with flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP and it launches another activity, where I want to place the exit button. In order to get everything to the background I have to finish both current and main activity. I though that launching main activity with those flags and extra info that it should exit would do the trick, but the extras delivered with the intent do not reach main activity - it still gets the intent that was used by android to launch the application.
In other words, I tried something like:
// Exit's onClick:
Intent intent = new Intent(someContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("exit", true);
context.startActivity(intent);
currentActivity.finish();
// MainActivity onCreate:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
// application never reach this point
boolean exit = extras.getBoolean("exit");
if (exit)
{
finish();
return;
}
}
The extras are delivered. How can I get it working?
Thanks