完成父母和Android的当前活动父母、Android

2023-09-12 03:01:23 作者:是人是狗笑着慢慢瞅

我有3个活动。活动一从而导致b活动,这反过来又可以返回到活动A或开始活动C.不过,如果我preSS回活性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活动 pressing 返回 b活动上应该导致A 在活动B开始活动ç pressing 返回上的活动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?这code现在给我一个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,开始活动B中使用的结果 startActivityForResult()

在活动B,当用户触发活动C,开始活动℃。

In Activity B, when the user triggers Activity C, start activity C.

startActivity()立即返回,所以

设置结果将告知完成为好,

set a result that will inform A to finish as well,

呼叫完成() B中

当A接收时产生的B,A呼叫完成()本身也是如此。

When A receives that result from B, A calls finish() on itself as well.

如果做不到这一点,你可以让活动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.

P.S。以Falmarri的评论考虑为你前进! 祝你好运。

P.S. Take Falmarri's comment into consideration as you move forward! Good luck.