如何将当前活动退出到主屏幕(不使用“主页”按钮)?
我确信这个问题将会得到解答,但事实证明我找不到它。所以请原谅我的冗余。
我想做的是模拟“Home”按钮,该按钮可以返回 Android 的主屏幕。这就是导致我出现问题的原因:
- 我有 3 个启动器活动。第一个(连接到主屏幕图标)只是一个(受密码保护的)配置活动。它不会被用户使用(仅管理员),
- 另外两个(均通过应用程序小部件访问)之一是问卷应用程序。我也允许通过后退按钮或 GUI 后退按钮在问题之间跳转。调查问卷完成后,我会总结给出的答案,并提供一个“完成”按钮,该按钮应将用户带回主屏幕。
对于问卷应用程序,我使用一个活动(称为 ItemActivity),它调用自身(使用意图时也是递归吗?)从一个问题跳转到另一个问题:
Questionnaire.serializeToXML();
Intent i = new Intent().setClass(c, ItemActivity.class);
if(Questionnaire.instance.getCurrentItemNo() == Questionnaire.instance.getAmountOfItems()) {
Questionnaire.instance.setCompleted(true);
} else Questionnaire.instance.nextItem();
startActivity(i);
最终屏幕显示类似“感谢您参与”以及前面描述的按钮应该将用户带回到主屏幕。但我真的不知道如何正确退出活动。例如,我使用了 this.finish();
但这奇怪地再次出现“谢谢”屏幕。那么我怎样才能通过跳回到主屏幕来退出呢?
抱歉给您带来不便。
问候,
斯特夫
I am sure this will have been answered but I proved unable to find it. So please excuse my redundancy.
What I am trying to do is emulating the "Home" button which takes one back to Android's homescreen. So here is what causes me problems:
- I have 3 launcher activities. The first one (which is connected to the homescreen icon) is just a (password protected) configuration activity. It will not be used by the user (just admin)
- One of the other 2 (both accessed via an app widget) is a questionnaire app. I'm allowing to jump back between questions via the Back button or a GUI back button as well. When the questionnaire is finished I sum up the answers given and provide a "Finish" button which should take the user back to the home screen.
For the questionnaire app I use a single activity (called ItemActivity) which calls itself (is that recursion as well when using intents?) to jump from one question to another:
Questionnaire.serializeToXML();
Intent i = new Intent().setClass(c, ItemActivity.class);
if(Questionnaire.instance.getCurrentItemNo() == Questionnaire.instance.getAmountOfItems()) {
Questionnaire.instance.setCompleted(true);
} else Questionnaire.instance.nextItem();
startActivity(i);
The final screen shows something like "Thank you for participating" as well as the formerly described button which should take one back to the homescreen. But I don't really get how to exit the Activity properly. I've e.g. used this.finish();
but this strangely brings up the "Thank you" screen again. So how can I just exit by jumping back to the homescreen??
Sorry for the inconvinience.
Regards,
Steff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来好像发生了这样的情况:您同时打开了多个活动副本。每次启动活动的新实例时,您只是将新实例添加到堆栈中 - 所有旧活动仍然存在。当您调用
this.finish()
时,它只是向您显示下一个最近打开的活动。我不确定为什么这是“谢谢”屏幕,因为大概只有在最后才会打开,但是我没有完全遵循您的活动被调用的顺序。您可以尝试修复它的方法是: 1) 每次针对不同问题开始新活动后,都使用
finish()
(这样一次只打开一个问题活动),或者 2) 看看是否可以利用意图标志 CLEAR_TOP。这将启动目标活动,同时杀死堆栈中可能位于其上方的任何内容。例如,如果您有活动:A、B、C、C、C、C、C(每个问题等),那么以 CLEAR_TOP 开始 A 将杀死 B 和 C 的所有实例。您可以将其放入您的程序结构中通过更改用户开始调查问卷时看到的第一个屏幕。默认情况下,这将显示一条“欢迎”消息,并带有一个开始调查问卷的按钮。但是,如果它以与意图捆绑在一起的特定值开始,它将显示“谢谢”消息。如果您这样做,那么一旦您
finish()
活动 A,用户将返回到启动您的应用程序之前的位置 - 如果使用应用程序小部件启动,则可能是主屏幕。It sounds like what's happening is that you've got multiple copies of your activities open at the same time. Every time you start a new instance of an activity you're just adding new ones to the stack - all the old activities are still there. When you call
this.finish()
, it's just showing you the next most recent activity that was open. I'm not sure why that's the "Thank you" screen since presumably that only gets opened at the end, but then I don't fully follow the sequence your activities get called in.What you can try doing to fix it is either: 1) use a
finish()
every time after starting a new activity for a different question (so that there's only ever one question activity open at a time), or 2) see if you can make use of the intent flag CLEAR_TOP. That starts the target activity while killing anything that might have been above it in the stack. For example if you have activities: A, B, C, C, C, C, C (etc for each question), then starting A with CLEAR_TOP will kill off all instances of B and C. You could fit this into your program structure by changing the first screen the user sees when they start a questionnaire. This would show a 'welcome' message by default with a button to start the questionnaire. However, if it's started with a particular value bundled with the intent, it would display the "Thank you" message.If you do that, then once you
finish()
activity A, the user will be returned to wherever they were before they started your app - presumably the homescreen if it's started with an appwidget.