There is an application consisting of many screens. On each screen there is a button to go to the activation from the main menu. How to make it so that after switching to the Home button from any activation, it will switch to the main activation, then by pressing the phone’s back button, exit the application, instead of pressing the back button 100 times, turning all activations on which come in to get out?
2 answers
Alternatively, you can redefine pressing the back button in the main activation and not terminating the activation, but emulate pressing the phone button on the HOME button . So the application will collapse, but will not show previous activations.
@Override public void onBackPressed() //эмулируем нажатие на HOME, сворачивая приложение Intent i = new Intent(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_HOME); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } In general, yes, it is better to learn why and how your activations remained in the stack and prevent this
Add this code to all activities except the main one.
@Override protected void onUserLeaveHint() { //код для перехода к вашей главной активности super.onUserLeaveHint(); } And so that when you click Back on the main activity, the output occurs add to it
@Override public void onBackPressed() finish(); } BUT
If during the transition to the main activity all the others are not closed ( finish() ), there will be no way out but return to it. Therefore, make sure that all your transitions to the main activity are accompanied by the code finish()
I advise you to read about the life cycle of the application
- Thank you, but I meant Button, which throws on the main activation. And about the Back button, thanks. - Lev Naumenko
- Is something else or the issue resolved? - Flippy
Intent'at "home" add flagsIntent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK- woesss