在 Android 中完成父活动和当前活动 [英] Finish parent and current activity in Android
问题描述
我有 3 个活动.活动 A 导致活动 B,活动 B 又可以返回活动 A 或启动活动 C.但是,如果我在活动 C 中按回,应用程序应该关闭.
I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should close.
总结:
- 活动 A 开始活动 B
- 在活动 B 上按返回应该会导致 A
- 活动 B 开始活动 C
- 在活动 C 上按返回应关闭应用
- Activity A starts activity B
- Pressing Back on activity B should lead to A
- Activity B starts activity C
- Pressing Back on activity C should close the app
我应该如何从活动 B 转到活动 C?这段代码目前在最后一行给了我一个 NullPointerException:
How should I go from activity B to C? This code currently gives me a NullPointerException on the last line:
Intent intent=new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
ActivityB.this.finish();
ActivityB.this.getParent().finish();
如果我切换最后两行,我也会得到一个空指针.
If I switch the last two lines I also get a null pointer.
推荐答案
我不知道这是否可行,但你可以试试:
I don't know if this will work, but you could try it:
从活动 A,使用
startActivityForResult()
在Activity B中,当用户触发Activity C时,启动Activity C.
In Activity B, when the user triggers Activity C, start activity C.
startActivity()
立即返回,所以
设置一个结果,通知A也完成,
set a result that will inform A to finish as well,
在 B 中调用 finish()
.
当 A 收到 B 的结果时,A 也会对自身调用 finish()
.
When A receives that result from B, A calls finish()
on itself as well.
否则,您可以将 Activity C 设置为自己的应用程序,然后在启动第二个应用程序后关闭第一个应用程序(使用 A 和 B).
Failing that, you could make Activity C into its own app and then close the first app (with A & B) after it starts the second.
附言在您前进的过程中考虑 Falmarri 的评论!
祝你好运.
P.S. Take Falmarri's comment into consideration as you move forward!
Good luck.
这篇关于在 Android 中完成父活动和当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!