简体   繁体   English

完成Android中的父级和当前活动

[英]Finish parent and current activity in Android

I have 3 activities. 我有3个活动。 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或开始活动C.但是,如果我在活动C中按回应用程序应该关闭。

To sum up: 总结一下:

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

How should I go from activity B to C? 我应该如何从活动B转到C? This code currently gives me a NullPointerException on the last line: 这段代码目前在最后一行给我一个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. 如果我切换最后两行,我也会得到一个空指针。

I don't know if this will work, but you could try it: 我不知道这是否有效,但你可以尝试一下:

  • From Activity A, start activity B for a result using startActivityForResult() 从活动A开始,使用startActivityForResult()为结果启动活动B.

  • In Activity B, when the user triggers Activity C, start activity C. 在活动B中,当用户触发活动C时,启动活动C.

startActivity() returns immediately, so startActivity()立即返回,所以

  • set a result that will inform A to finish as well, 设置一个结果,通知A也完成,

  • Call finish() in B. 在B中调用finish()

  • When A receives that result from B, A calls finish() on itself as well. 当A从B接收到该结果时,A也会调用finish()

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)。

PS Take Falmarri's comment into consideration as you move forward! PS在你前进的过程中考虑Falmarri的评论!
Good luck. 祝好运。

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. 请注意,我已在子活动中将值设置为2,并在父活动中检查它。 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. 然后,您可以使用递归方法进行进一步的活动。

You shou use onActivityResult method in your parent Activity 您应该在父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) 假设活动A是活动B的父级。如果要在活动B中单击后退按钮以退出应用程序(也退出活动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 onActivityResult method inside the method you can call 你的父Actvity A,在你可以调用的方法中监听你需要使用onActivityResult方法的结果代码

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

It works for me :) 这个对我有用 :)

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();
}

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. 帮助清楚地列出了Intent.FLAG_ACTIVITY_CLEAR_TASK必须与Intent.FLAG_ACTIVITY_NEW_TASK一起使用。 So here is a code that worked for me. 所以这是一个适合我的代码。

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

try this 试试这个

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

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也将完成。

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将是根活动,然后按下将完成应用程序。

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中的后面将退出应用程序。

From Activity A, start activity B for a result using startActivityForResult(intent,19) 从活动A开始,使用startActivityForResult(intent,19)为结果启动活动B.

In Activity B, when the user triggers Activity C, start activity C. 在活动B中,当用户触发活动C时,启动活动C.

startActivity() returns immediately, so startActivity()立即返回,所以

set a result that will inform A to finish as well, 设置一个结果,通知A也完成,

Call finish() in B. and Call setResult() method before finish() method in your Child Activity In Parent Activity overide onActivtyResult(....) method. 在B.和调用setResult()方法之前调用setResult()方法在您的Child Activity In Parent Activity中覆盖onActivtyResult(....)方法。 When A receives that result from B, A calls finish() on itself as well. 当A从B接收到该结果时,A也会调用finish()。

enter code here
     @Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
    //Added by Ashish to close parent activity
      if(arg1==19){
      finish();
        }
    super.onActivityResult(arg0, arg1, arg2);
}

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 !! 希望能帮助到你 !!

This is working perfectly: 这完美地运作:

btn_back.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {

Player.this.finish();
    }
});

You can just override the back key behavior : 您可以覆盖后退键行为

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

Note that I don't call super from onBackPressed. 请注意,我不会从onBackPressed调用super。 This also allows you to override the browse stack mechanism for Android, which I find to be a big advantage, since currently there doesn't seem to be a way to clear the single oldest item from the browse stack in a simple way. 这也允许你覆盖Android的浏览堆栈机制,我认为这是一个很大的优势,因为目前似乎没有办法以简单的方式从浏览堆栈中清除单个最旧的项目。

public class B extends Activity
{
    ...
    @Override
    public void onBackPressed()
    {
        // Start activity A
        Intent aIntent = new Intent(this, A);
        startActivity(bIntent);
    }
    ...
}

public class C extends Activity
{
    ...
    @Override
    public void onBackPressed()
    {
        // Close
        finish();
    }
    ...
}

You can specifically finish the parent activity if needed too, but using this method, you don't have to worry about it. 如果需要,您也可以专门完成父活动,但使用此方法,您不必担心它。

The following code should do it for you. 以下代码应该为您完成。 Almost in all other approaches your app will remain in recent apps. 几乎在所有其他方法中,您的应用将保留在最近的应用中。 Javadoc can be found at, https://developer.android.com/reference/android/app/ActivityManager.AppTask.html#finishAndRemoveTask() Javadoc可以在https://developer.android.com/reference/android/app/ActivityManager.AppTask.html#finishAndRemoveTask()找到

            ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
            if(am != null) {
                List<ActivityManager.AppTask> tasks = am.getAppTasks();
                if (tasks != null) {
                    tasks.get(0).finishAndRemoveTask();
                }
            }

Have each activity listen for a result from the next activity, and finish itself if that occurs. 让每个活动都听取下一个活动的结果,并在发生这种情况时自行完成。

You can use a special result code to indicate that you want the activity to finish. 您可以使用特殊结果代码来指示您希望活动完成。

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

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