在 Android 中完成父活动和当前活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3410295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:04:34  来源:igfitidea点击:

Finish parent and current activity in Android

androidandroid-activity

提问by hpique

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.

我有3个活动。活动 A 导致活动 B,而活动 B 又可以返回活动 A 或启动活动 C。但是,如果我在活动 C 中按回,应用程序应该关闭。

To sum up:

总结:

  • Activity A starts activity B
  • Pressing Backon activity B should lead to A
  • Activity B starts activity C
  • Pressing Backon activity C should close the app
  • 活动 A 开始活动 B
  • 活动 B 应该导致 A
  • 活动 B 开始活动 C
  • 返回活动 C 应关闭应用程序

How should I go from activity B to C? This code currently gives me a NullPointerException on the last line:

我应该如何从活动 B 转到活动 C?这段代码目前在最后一行给了我一个 NullPointerException :

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.

如果我切换最后两行,我也会得到一个空指针。

回答by codinguser

I don't know if this will work, but you could try it:

我不知道这是否可行,但您可以尝试一下:

  • From Activity A, start activity B for a result using startActivityForResult()

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

  • 从活动 A,开始活动 B 以获得结果,使用 startActivityForResult()

  • 在Activity B中,当用户触发Activity C时,启动Activity C。

startActivity()returns immediately, so

startActivity()立即返回,所以

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

  • Call finish()in B.

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

  • 设置一个结果也会通知 A 完成,

  • 呼叫finish()B。

  • 当 A 从 B 收到该结果时,Afinish()也会调用自己。

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.

否则,您可以将 Activity C 设为它自己的应用程序,然后在它启动第二个应用程序后关闭第一个应用程序(使用 A 和 B)。

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

PS 在您前进时考虑 Falmarri 的评论!
祝你好运。

回答by Anuj Agarwal

If you want to finish a parent activity from a child activity,

如果要从子活动完成父活动,

In the parent activity, while triggering the child activity, use the following command:-

在父活动中,在触发子活动时,使用以下命令:-

startActivityForResult(intent_name,any_integer_variable);

and override the onActivityResult Method in the following manner:-

并以下列方式覆盖 onActivityResult 方法:-

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode==2){
        finish();
    }
}

Now, in the child activity, override onStop and onDestroy in the following manner:-

现在,在子活动中,按以下方式覆盖 onStop 和 onDestroy:-

protected void onStop() {
    setResult(2);
    super.onStop();
}
@Override
protected void onDestroy() {
    setResult(2);
    super.onDestroy();
}

Notice that I've set the value to 2 in the child activity, and am checking for it in the parent activity. If the value is the same that I have set, then the parent activity will also finish. Then you can use recursive approaches for further activities.

请注意,我已在子活动中将值设置为 2,并在父活动中检查它。如果该值与我设置的值相同,则父活动也将完成。然后您可以使用递归方法进行进一步的活动。

回答by TS.xy

You shou use onActivityResultmethod in your parent Activity

你应该在你的父活动中使用onActivityResult方法

Suppose Activity A is parent of Activity B. If you want to click back button in Activity B to exit Application (also exit Activity A)

假设Activity A是Activity B的父级,如果你想点击Activity B中的后退按钮退出Application(也退出Activity A)

In your Activity B, in onStop()or onDestroy()

在您的活动 B 中,onStop()onDestroy()

you call

你打电话

setResult(0); //any int number is fine

this will pass a result code to its parent activity.

这会将结果代码传递给其父活动。

Your parent Actvity A, listens for the result code you will need to use onActivityResultmethod inside the method you can call

您的父活动 A,侦听您需要onActivityResult在可以调用的方法中使用方法的结果代码

if(resultCode == 0) //matches the result code passed from B
{
    ActivityA.this.finish()
}

It works for me :)

这个对我有用 :)

回答by Yugank

None of that worked for me. So I found out a solution. The help clearly lists that Intent.FLAG_ACTIVITY_CLEAR_TASK must be used with Intent.FLAG_ACTIVITY_NEW_TASK. So here is a code that worked for me.

这些都不适合我。于是我找到了解决办法。帮助中明确列出了 Intent.FLAG_ACTIVITY_CLEAR_TASK 必须与 Intent.FLAG_ACTIVITY_NEW_TASK 一起使用。所以这是一个对我有用的代码。

Intent intent=new Intent(ActivityB.this, ActivityC.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

回答by LifesAway

I didn't do any of this. This is how I would revise your code.

我没有做任何这些。这就是我将如何修改您的代码。

the code you use to enter another intent:

您用于输入另一个意图的代码:

Intent whatEverIntentName = new Intent("Path.to.next.Intent");
startActivity(whatEverIntentName);
finish();

This way, you always quit when pressing back. But wait! You can change how you want your back key press to react when pressed.

这样,你总是在按下时退出。可是等等!您可以更改后退键按下时的反应方式。

Do this:

做这个:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent whatEverIntentName = new Intent("Path.to.the.activity.before.it");
    startActivity(whatEverIntentName);
// Don't add finish here. 
//This is necessary because you finished your last activity with finish();
}

回答by Animesh Mangla

You can use finishAffinity(), it will close Current & all Parent Activities of the Current Activity.

您可以使用finishAffinity(),它将关闭当前活动的当前和所有父活动。

Note : But any results from Child Activity to Parent Activity will not be handled.

注意:但不会处理从子活动到父活动的任何结果。

Hope, it helps !!

希望能帮助到你 !!

回答by Ali Shan

if you are on Activity A and started another activity called Activity B. Activity A --> Activity B

如果您在活动 A 上并启动了另一个名为活动 B 的活动。活动 A --> 活动 B

And now want to close both activities.

现在想关闭这两个活动。

then use the below code in Activity B.

然后在活动 B 中使用以下代码。

B.this.finish();

And use this code in Activity A:

并在活动 A 中使用此代码:

startActivity(new Intent(A.this, B.class));
finish();

As soon as activity B will finish, Activity A will also finish.

一旦活动 B 完成,活动 A 也将完成。

There is one drawback, I do not know may be you want this or not.

有一个缺点,我不知道你是否想要这个。

Drawback is that If user is on Activity B and s/he press back button to move on the Activity A. Then, Activity A will also finish in this case.

缺点是如果用户在活动 B 上并且他/她按下后退按钮在活动 A 上移动。那么,在这种情况下,活动 A 也将完成。

回答by Ketan Ahir

try this

尝试这个

  Intent intent = new Intent(Intent.ACTION_MAIN);
  intent.addCategory(Intent.CATEGORY_HOME);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);                

回答by Vipin Sahu

Intent intent=new Intent(ActivityB.this, ActivityC.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

your activity C will the root activity and on press back will finish the apps.

您的活动 C 将成为根活动,按回将完成应用程序。

回答by miroslavign

From the screen(B) you want to start the screen(C) with NO back screen, just start this screen (C) with:

从屏幕(B)你想启动没有后屏的屏幕(C),只需启动这个屏幕(C):

screenC.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
    | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(screenC);
finish();

The back in screenC will exit the application.

返回 screenC 将退出应用程序。