如何保持单个活动实例但能够更新视图? [英] How to keep a single activity instance but be able to update the view?
问题描述
在我的情况下,我需要确保该活动一次只运行一次.
In my situation, there is one case in which I need to make sure the activity only runs one at a time.
我发现如果我设置活动的LauchMode
,我可以达到单实例目标,但是它不会更新活动的视图.
I found if I set the LauchMode
of the activity, I can reach the single instance aim, but it won't update the view of the activity.
此活动由startActivityForResult
启动,我们将带有意图的URI发送到该活动.
This activity is launched by startActivityForResult
, and we send the URI with the intent to the activity.
让我们讨论这种情况:
-
画廊-使用imageA禁用此活动.
gallery - lauch this activity with imageA.
相机-使用imageB禁用此活动.
camera - lauch this activity with imageB.
我的要求不是销毁旧活动,而是刚刚收到新意图信息的活动应刷新视图.
My request is not to destroy the old activity, but the activity that just received the new intent infomation should refresh the view.
我找到了一种新方法onNewIntent
.
此方法可以在恢复之前刷新意图.我会尝试的.
I found a new method, onNewIntent
.
This method can refresh the intent before resume. I will try it.
推荐答案
您可以拥有一个具有清单清单属性为SingleInstance的Activity. 重新启动活动后,就会调用onResume.您可以使用新图像更新视图,并使旧的视图无效.
You can have an Activity with a manifest attribute of singleInstance. As soon as the activity is relaunched , onResume gets called. You can update the view with the new image and invalidate the older View.
<activity ..
android:launchMode= "singleInstance" />
这篇关于如何保持单个活动实例但能够更新视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!