简体   繁体   English

在Android内,如何开始一项活动,或者如果该活动已经在堆栈中,那么该活动如何发挥作用?

[英]Within Android, how do I start an activity or bring to front that activity if already on stack?

Within Android, how do I start an activity or bring to front that activity if already on stack? 在Android内,如何开始一项活动,或者如果该活动已经在堆栈中,那么该活动如何发挥作用?

So far I know start a new activity myActivity , as by the code below shows. 到目前为止,我知道开始一个新的活动myActivity ,如下面的代码所示。 But I don't want to open a second copy of the same class, if it's already open on the stack. 但是我不想打开同一类的第二个副本,如果它已经在堆栈上打开了。 So how do I check whether activities like the below exist on the stack, then if it exists bring it to front. 因此,我如何检查堆栈中是否存在以下活动,如果存在,将其置于最前面。 If it doesn't, open a new activity like below 如果没有,请打开一个新的活动,如下所示

Intent intent = new Intent(myActivity.this,
        myActivity.class);
startActivity(intent);

Use a flag in your intent when you want to start new Activity 当您要开始新的活动时,请在意图中使用标记

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Intent intent = new Intent(myActivity.this,
        myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Intent intent = new Intent(myActivity.this,
        myActivity.class);
 intent .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

It sounds like you might have several processes running or other instances of the Activity started through other calls to start the Activity. 听起来您可能正在运行多个进程,或者通过其他调用来启动Activity的其他实例。

You are probably needing to specify in the manifest either "singleInstance" or "singleTask" - 您可能需要在清单中指定“ singleInstance”或“ singleTask”-

Android singleTask or singleInstance launch mode? Android singleTask或singleInstance启动模式?

Word of caution - this may have some other unintended side effects. 温馨提示-这可能会带来其他一些意想不到的副作用。 Just be sure to test it for different use-cases. 只要确保针对不同的用例进行测试即可。

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

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