简体   繁体   English

通过意图切换到另一个活动时如何销毁一个活动?

[英]How to destroy an activity when switching to another activity via intent?

I have 3 activities A, B and C. Activity A has a button which when clicked takes me to Activity B. Activity B has a button which when clicked takes me to Activity C. When I click on button in Activity A it takes me to Activity B but Activity A is paused and not destroyed and the same happens when I click Activity B's button and go to acitivty C. I tried using finish() on activity B, but the problem is when I press the back button in activity C it goes directly to Activity A. What I want is when I click Activity B's button it should destroy Acitivity B and take me to Acitivity C and in Activity C, if I press back button it should take me to newly created activity B 我有3个活动A,B和C。活动A有一个按钮,单击该按钮可将我带到活动B。活动B有一个按钮,单击该按钮可将我带到活动C。单击活动A中的按钮可将我带到活动B。活动B,但活动A已暂停且未被破坏,当我单击活动B的按钮并转到活动C时,也会发生相同的情况。我尝试在活动B上使用finish(),但是问题是当我在活动C中按返回按钮时直接转到活动A。我想要的是,当我单击活动B的按钮时,它应该破坏Acitivity B并带我进入Acitivity C;在活动C中,如果按向后按钮,它将带我进入新创建的活动B

Activity A -> Activity B-> Activity C 活动A->活动B->活动C

You'll be working against the Android framework. 您将使用Android框架。 That leads to frustrations. 这导致挫败感。 Instead, when you go to activity C, don't finish activity B. Instead, in activity B in onResume(), do something based on the fact that you went to activity C. Or starting Activity C using startActivityForResult() may be appropriate. 相反,当您进入活动C时,不要完成活动B。相反,在onResume()中的活动B中,根据您进入活动C的事实进行操作。或者使用startActivityForResult()启动活动C可能是适当的。

Edited 已编辑

The problem is updating the UI after something changed in activity C. The best place to do that in activity B is in the onResume(). 问题是在活动C中进行了某些更改之后更新了UI。在活动B中进行此操作的最佳位置是onResume()。 Put the logic for updating the UI in onResume() instead of in onCreate(). 将用于更新UI的逻辑放在onResume()中, 而不是放在onCreate()中。 When an activity is created, onCreate() is invoked, and onResume() is also invoked. 创建活动时,将调用onCreate(),并且还将调用onResume()。

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

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