I'm finding that in my application, the user can get quite 'nested' in the various activities that are opened while the user is using the application.
我发现在我的应用程序中,用户可以在用户使用应用程序时打开的各种活动中得到“嵌套”。
For example:
- Main Menu
- Object List
- Object Details
- Object Edit
- Object Details
- Object Child Details
- Object Child Edit
- Object Child Details
对象子细节
对象子编辑
对象子细节
Now, when the user presses back, it has to go through 'Object Child Details' twice (same object, when it is edited it returns to the detailed page), and the same thing happens for the 'Parent Object Details'.
现在,当用户按下时,它必须经历两次“对象子细节”(相同的对象,当它被编辑时返回到详细页面),并且“父对象细节”也会发生同样的事情。
Is there a way to reuse activities, if they are already open in the stack, and reorder them to the front? The only way I have seen is on activities with the launcher
attribute. I believe I saw singleTask
and singleTop
.
有没有办法重用活动,如果它们已经在堆栈中打开,并将它们重新排序到前面?我看到的唯一方法是使用launcher属性进行活动。我相信我看到了singleTask和singleTop。
If am supposed to be using these two attributes, singleTask
and singleTop
, how am I supposed to use them? When I tried to include them in the application, it made no difference. Do I also need to set a flag while launching the intent using startActivity
?
如果我应该使用这两个属性,singleTask和singleTop,我该如何使用它们?当我试图将它们包含在应用程序中时,它没有任何区别。使用startActivity启动intent时是否还需要设置标志?
7 个解决方案
#1
33
in Manifest Activity property you can give this parameter android:launchMode="singleInstance"
在Manifest Activity属性中你可以给这个参数android:launchMode =“singleInstance”
Read in more detail here http://developer.android.com/guide/topics/manifest/activity-element.html
请在此处阅读更多详细信息,请访问http://developer.android.com/guide/topics/manifest/activity-element.html
#2
22
This is your flag! 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.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
Note the 'addFlags'. Also note that, onCreate
will not be called on this Activity when a new Intent is delivered to it. It will be delivered via the onNewIntent()
.
注意'addFlags'。另请注意,当向其传递新的Intent时,不会在此Activity上调用onCreate。它将通过onNewIntent()传递。
This does not ensure that there is a single instance of the Activity running. To ensure that, check this out: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
这不能确保活动的单个实例正在运行。为了确保这一点,请查看:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
#3
16
Using android:launchMode="singleTask" is probably the best approach, since it won't recreate the activity if it's already running. Just add it to the activity in your AndroidManifest.xml, and you should be all set.
使用android:launchMode =“singleTask”可能是最好的方法,因为如果活动已经运行,它将不会重新创建活动。只需将其添加到AndroidManifest.xml中的活动中,您就应该全部设置。
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here's another question that might be useful: Android singleTask or singleInstance launch mode?
这是另一个可能有用的问题:Android singleTask或singleInstance启动模式?
#4
7
Yes you can demand only one instance of these activities be created, but it is generally not recommended. If you simply are concerned about history, take a look at Intent.FLAG_ACTIVITY_CLEAR_TOP.
是的,您只能要求创建这些活动的一个实例,但通常不建议这样做。如果您只关心历史,请查看Intent.FLAG_ACTIVITY_CLEAR_TOP。
#5
4
This worked for me.
这对我有用。
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
If an instance of this Activity already exists, then it will be moved to the front. If an instance does NOT exist, a new instance will be created.
如果此活动的实例已存在,则它将移至前面。如果实例不存在,则将创建新实例。
#6
1
Add intent Flags as
添加意图标志为
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
StartActivity(srcActivity.java, DesiredActivity.class);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); StartActivity(srcActivity.java,DesiredActivity.class);
Then at onPause()
DesiredActivity
然后在onPause()DesiredActivity
Add finish()
, This did the work for me.
添加完成(),这为我做了工作。
#7
0
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
It works for me :)
这个对我有用 :)
#1
33
in Manifest Activity property you can give this parameter android:launchMode="singleInstance"
在Manifest Activity属性中你可以给这个参数android:launchMode =“singleInstance”
Read in more detail here http://developer.android.com/guide/topics/manifest/activity-element.html
请在此处阅读更多详细信息,请访问http://developer.android.com/guide/topics/manifest/activity-element.html
#2
22
This is your flag! 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.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
Note the 'addFlags'. Also note that, onCreate
will not be called on this Activity when a new Intent is delivered to it. It will be delivered via the onNewIntent()
.
注意'addFlags'。另请注意,当向其传递新的Intent时,不会在此Activity上调用onCreate。它将通过onNewIntent()传递。
This does not ensure that there is a single instance of the Activity running. To ensure that, check this out: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
这不能确保活动的单个实例正在运行。为了确保这一点,请查看:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
#3
16
Using android:launchMode="singleTask" is probably the best approach, since it won't recreate the activity if it's already running. Just add it to the activity in your AndroidManifest.xml, and you should be all set.
使用android:launchMode =“singleTask”可能是最好的方法,因为如果活动已经运行,它将不会重新创建活动。只需将其添加到AndroidManifest.xml中的活动中,您就应该全部设置。
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here's another question that might be useful: Android singleTask or singleInstance launch mode?
这是另一个可能有用的问题:Android singleTask或singleInstance启动模式?
#4
7
Yes you can demand only one instance of these activities be created, but it is generally not recommended. If you simply are concerned about history, take a look at Intent.FLAG_ACTIVITY_CLEAR_TOP.
是的,您只能要求创建这些活动的一个实例,但通常不建议这样做。如果您只关心历史,请查看Intent.FLAG_ACTIVITY_CLEAR_TOP。
#5
4
This worked for me.
这对我有用。
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
If an instance of this Activity already exists, then it will be moved to the front. If an instance does NOT exist, a new instance will be created.
如果此活动的实例已存在,则它将移至前面。如果实例不存在,则将创建新实例。
#6
1
Add intent Flags as
添加意图标志为
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
StartActivity(srcActivity.java, DesiredActivity.class);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); StartActivity(srcActivity.java,DesiredActivity.class);
Then at onPause()
DesiredActivity
然后在onPause()DesiredActivity
Add finish()
, This did the work for me.
添加完成(),这为我做了工作。
#7
0
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
It works for me :)
这个对我有用 :)