简体   繁体   English

如何同时完成()两项活动?

[英]How to finish() two activities at the same time?

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 ). 它首先提示用户选择他们想要的问题类型( MainActivity ),然后它会显示一堆问题( QuestionsActivity )。 After answering 10 questions, it tells you which question(s) did you answer correctly and which you didn't (ResultsActivity). 在回答了10个问题之后,它会告诉您哪些问题得到了正确回答,哪些问题没回答(ResultsActivity)。

I know that Android puts all the activities on a stack. 我知道Android将所有活动放在堆栈上。 In my case, it would look like this: 就我而言,它看起来像这样:

ResultsActivity
QuestionsActivity
MainActivity

And when you call finish , an activity is popped from the stack. 当你调用finish ,会从堆栈中弹出一个活动。 I want there to be a back to main menu button in the ResultsActivity to go back to the MainActivity . 我希望在ResultsActivity返回主菜单按钮以返回MainActivity However, if I call finish in the ResultsActivity , the user would see QuestionsActivity ! 但是,如果我在ResultsActivity调用finish ,则用户将看到QuestionsActivity So how am I going to call finish on the both activities? 那我怎么打电话给两个活动finish

Two options: 两种选择:

  1. Call finish() in QuestionsActivity after you make the call to start the ResultsActivity. 在调用启动ResultsActivity之后,在QuestionsActivity中调用finish() This will remove it from the stack so that pressing back from ResultsActivity returns to MainActivity. 这将从堆栈中删除它,以便从ResultsActivity返回返回MainActivity。
  2. Use Intent.FLAG_ACTIVITY_CLEAR_TOP in the intent to go back to MainActivity. 在意图中使用Intent.FLAG_ACTIVITY_CLEAR_TOP返回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: 您可以通过再次简单地启动MainActivity并使用以下标志清除堆栈来清除堆栈:

  final Intent intent = new Intent(context, MainActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM