简体   繁体   English

Android-如何返回以前的其他活动

[英]Android - how to return to different previous activities

I have several activities/pages in my app. 我的应用程序中有几个活动/页面。 From the first activity, I can open a second activity. 从第一个活动中,我可以打开第二个活动。 In this second activity a game is played. 在此第二活动中,玩游戏。 Once this game ends, a new activity starts. 游戏结束后,新的活动开始。 In that new activity, I have a button that finishes it, using finish() , which returns me to the activity with the game. 在该新活动中,我有一个使用finish()完成该按钮的按钮,它使我回到游戏的活动中。 I also have a button that is to return it to the very first activity. 我还具有一个将其返回到第一个活动的按钮。 Here is a diagram to clarify: 这是一个说明图:

img

When I use finish() to go back to the Game Activity, the game is in an endstate. 当我使用finish()返回游戏活动时,游戏处于结束状态。 I want to reset all variables and widgets, but I do not know how to check if I entered this page via calling finish() in the Final Activity. 我想重置所有变量和小部件,但是我不知道如何通过在Final Activity中调用finish()来检查是否进入了此页面。

Q1: How can I check if I entered an activity as a result of finishing another? 问题1:如何检查是否由于完成另一个活动而参加了一项活动?

I also have another question- what method can I use in button2 to finish not only the final activity but also all previous activities on the stack (except for the first activity, so in this case it would be Final Activity and Game Activity), in order to return to the first activity? 我还有一个问题:在button2中,我可以使用什么方法不仅可以完成最终活动,还可以完成堆栈上的所有先前活动(除了第一个活动,因此在本例中将是“最终活动”和“游戏活动”),为了返回第一个活动? In other answers on stackoverflow, I have found suggestions to use FLAG_ACTIVITY_CLEAR_TOP , but if I were to do that, I could no longer also return to Game Activity with button1. 在关于stackoverflow的其他答案中,我找到了使用FLAG_ACTIVITY_CLEAR_TOP建议,但如果这样做,我将无法再通过button1返回到Game Activity。

Q2: How can I return to the first activity, while still allowing me to use finish() to return to other activities? 问题2:如何返回第一个活动,同时仍然允许我使用finish()返回其他活动?


Code I use to go back to previous activity: 我用来返回上一活动的代码:

// called from button1's onClick 
public void goBack(View button1){
    finish();
}

And for going back to the first activity: 回到第一个活动:

public void toFirstActivity(View button2){
    Intent previousPage = new Intent();
    previousPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    finish();
}

Q1: How can I check if I entered an activity as a result of finishing another? 问题1:如何检查是否由于完成另一个活动而参加了一项活动?

You can easily use a global boolean flag such as "activityFinished", when you call finish() on Final Activity, you set it to true. 您可以轻松地使用全局布尔标志,例如“ activityFinished”,当您在Final Activity上调用finish()时,将其设置为true。 and in onResume() of your activities, check if activityFinished == true, if so do your stuff and reset the flag to false. 并在您活动的onResume()中,检查activityFinished == true,如果是,则将您的内容设置为false。

Q2: How can I return to the first activity, while still allowing me to use finish() to return to other activities? 问题2:如何返回第一个活动,同时仍然允许我使用finish()返回其他活动?

I don't understand why FLAG_ACTIVITY_CLEAR_TOP is a problem for you. 我不明白为什么FLAG_ACTIVITY_CLEAR_TOP对您来说是个问题。 You only set the intent as FLAG_ACTIVITY_CLEAR_TOP to go back to your very first activity after you have pressed button 2, don't you? 您仅将意图设置为FLAG_ACTIVITY_CLEAR_TOP即可在按下按钮2后返回到您的第一个活动,不是吗? How does this have something to do with your button 1's action? 这与按钮1的动作有什么关系?

Update: try this code 更新:尝试此代码

Intent a = new Intent(this,FirstActivity.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);

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

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