I am writing a math app for little kids to learn maths. It first prompts the user to select what kind of questions they want (MainActivity
), and then it shows a bunch of questions (QuestionsActivity
). After answering 10 questions, it tells you which question(s) did you answer correctly and which you didn't (ResultsActivity).
I know that Android puts all the activities on a stack. In my case, it would look like this:
ResultsActivity
QuestionsActivity
MainActivity
And when you call finish
, an activity is popped from the stack. I want there to be a back to main menu button in the ResultsActivity
to go back to the MainActivity
. However, if I call finish
in the ResultsActivity
, the user would see QuestionsActivity
! So how am I going to call finish
on the both activities?
When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
finishAffinity() : finishAffinity() is not used to "shutdown an application". It is used to remove a number of Activities belonging to a specific application from the current task (which may contain Activities belonging to multiple applications).
Two options:
finish()
in QuestionsActivity after you make the call to start the ResultsActivity. This will remove it from the stack so that pressing back from ResultsActivity returns to MainActivity.Intent.FLAG_ACTIVITY_CLEAR_TOP
in the intent to go back to MainActivity. This will clear all activities that are on top of it.You can clear your stack by simple starting your MainActivity again and clearing the stack with the following flags:
final Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
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