简体   繁体   English

Android:如何避免启动已经在堆栈中的活动?

[英]Android: How do I avoid starting activity which is already in stack?

Lets try to explain my question: I got an application and a service. 让我们试着解释一下我的问题:我得到了一个应用程序和一个服务。 The application is started with activity A. The service sends a broadcast that will let the application start Activity B Now the user starts activity C. 应用程序以活动A启动。服务发送一个广播,让应用程序启动活动B现在用户启动活动C.

Now the service wants to start activity B again. 现在该服务想再次启动活动B. But how do I let him know that the activity is still on the stack, or is there an intent flag for this? 但是我怎么让他知道活动仍然在堆栈上,或者是否有一个意图标志?

How do I avoid that it will launch activity B because its already in the stack? 我如何避免它会启动活动B,因为它已经在堆栈中?

I think you need to make your activity B singleInstance that if it's already create you don't want to create again, that is launch mode of the activity can be defined in manifest android:launchMode that defines how the activity will be instanciated . 我认为你需要让你的活动B singleInstance ,如果它已经创建你不想再次创建,那么活动的启动模式可以在清单android:launchMode中定义,它定义了活动如何实现。

in your case use android:launchMode="singleInstance" 在你的情况下使用android:launchMode="singleInstance"

You can use flag Intent.FLAG_ACTIVITY_NEW_TASK . 您可以使用标志Intent.FLAG_ACTIVITY_NEW_TASK If the activity is already running it will bring that to front instead of creating new activity. 如果活动已经在运行,它将把它带到前面而不是创建新活动。

If you add Intent.FLAG_ACTIVITY_CLEAR_TOP with this, then all the activities after this activity in the backstack will be cleared. 如果您使用此添加Intent.FLAG_ACTIVITY_CLEAR_TOP ,则将清除此堆栈中此活动之后的所有活动。

If the activity will be on the top if already started, set the FLAG_ACTIVITY_SINGLE_TOP flag 如果活动将在已经开始时位于顶部,请设置FLAG_ACTIVITY_SINGLE_TOP标志

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);

Approaches android:launchMode="singleInstance" and just adding flags to the Intent do not work for me. 方法android:launchMode="singleInstance" ,只是向Intent添加标志对我不起作用。 What works is that: 有效的是:

In the code where activity gets started: 在活动开始的代码中:

Intent intent = new Intent(activity, ActivityThatHasToBeStarted.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);

In the ActivityThatHasToBeStarted : ActivityThatHasToBeStarted

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
        finish();
        return;
    }
    // Code for this creation
}

If you don't need the second activity anymore, it is good practise to finish it, then you should do this on the second activity after the operation is ended: 如果您不再需要第二个活动,最好完成它,然后您应该在操作结束后对第二个活动执行此操作:

startActivity(new Intent(this, FirstActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();

您可以考虑使用android:launchMode="singleTop"而不是android:launchMode="singleInstance" 关于差异的好文章

I would suggest you use Intent.FLAG_ACTIVITY_CLEAR_TOP as it will remove all activities which start on top of your targeted activity. 我建议您使用Intent.FLAG_ACTIVITY_CLEAR_TOP ,因为它将删除所有从您的目标活动开始的活动。

eg : 例如:

Intent intent = new Intent(sourceActivity, Target activity);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent)

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

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