[英]Android - startActivityForResult immediately triggering onActivityResult
I am launching activities from the main activity in my app using the call startActivityForResult(intent, ACTIVITY_TYPE)
, and they are all working but one.我正在使用调用startActivityForResult(intent, ACTIVITY_TYPE)
从我的应用程序中的主要活动启动活动,并且它们都在工作,但只有一个。
This one, when called, launches the activity as desired, but in the log I can see that onActivityResult()
is immediately being triggered.这个在调用时会根据需要启动活动,但在日志中我可以看到onActivityResult()
立即被触发。 The activity shows up but RESULT_CANCELED
is immediately returned to onActivityResult()
.活动出现但RESULT_CANCELED
立即返回到onActivityResult()
。
I then interact with the activity, press a button which calls finish()
, and onActivityResult()
is not called this time (because apparently a result has already been returned).然后我与活动交互,按下一个调用finish()
的按钮,这次没有调用onActivityResult()
(因为显然已经返回了结果)。
Does this make sense to anyone?这对任何人都有意义吗? Has anyone seen this behavior before?有没有人见过这种行为?
You can't use startActivityForResult()
if your activity is being launched as a singleInstance
or singleTask
.如果您的活动作为singleInstance
或singleTask
启动,则不能使用startActivityForResult()
。 standard
or singleTop
launch mode will fix the problem. standard
或单singleTop
启动模式将解决问题。
I have seen this behavior before, please make sure your destnation activity (that special activity) is not singleInstance
in AndroidManifest file.我以前见过这种行为,请确保您的目标活动(该特殊活动)不是 AndroidManifest 文件中的singleInstance
。 If the Activity is singleInstance
, then it will return RESULT_CANCELED
before launched!如果 Activity 是singleInstance
,那么它会在启动前返回RESULT_CANCELED
!
I'd also like to add that you could call an external app with:我还想补充一点,您可以通过以下方式调用外部应用程序:
Intent in = caller.getPackageManager().getLaunchIntentForPackage("com.your.package.here");
Which would create an intent with Intent.FLAG_ACTIVITY_NEW_TASK
added by default, so call:这将创建一个Intent.FLAG_ACTIVITY_NEW_TASK
默认添加的意图,因此调用:
in.setFlags(0);
Which will clear that flag, and then you can proceed to: startActivityForResult(in, action);
这将清除该标志,然后您可以继续: startActivityForResult(in, action);
Reason I'm doing this is that I have a utility app that has common functionality between a few other apps and I can keep the code changes to one location instead of worrying about multiple updates.我这样做的原因是我有一个实用程序应用程序,它在几个其他应用程序之间具有通用功能,我可以将代码更改保留在一个位置,而不必担心多次更新。
startActivityForResult()
doesn't work with a singleInstance
or singleTask
activity in pre-lollipop version of Android. startActivityForResult()
不适用于 Android 棒棒糖前版本中的singleInstance
或singleTask
活动。 Since Android 5 it works (see this answer for more details).由于 Android 5 它可以工作(有关更多详细信息,请参阅此答案)。
It also triggers if you have FLAG_ACTIVITY_NEW_TASK
in your intent.如果您的意图中有FLAG_ACTIVITY_NEW_TASK
,它也会触发。
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_CODE);
My problem was with the calling activity.我的问题是呼叫活动。 Its declaration in the AndroidManifest had the following property:它在 AndroidManifest 中的声明具有以下属性:
android:noHistory="true"
Changed it to "false" and now works fine.将其更改为“false”,现在工作正常。
Android 4.4 has a small problem about waiting for the return at the end of the actvity closure To solve this behavior it is important to set : Android 4.4 有一个关于在活动关闭结束时等待返回的小问题要解决此行为,重要的是设置:
TaskAffinity = "[SAME STRING]"
) ( TaskAffinity = "[SAME STRING]"
)launchmode=singleTop,
launchIntent.SetFlags(0); // for reset default Intent flags if you launch from package manager
This solution works with all version of Android此解决方案适用于所有版本的 Android
See this for taskAffinity: https://asyoulook.com/computers%20&%20internet/android-onactivityresult-being-called-instantly/1004072参见 taskAffinity: https ://asyoulook.com/computers%20&%20internet/android-onactivityresult-being-called-instantly/1004072
另外,检查清单中的活动是否 android:noHistory="true" ,如果是,它将不起作用。
For ActivityGroup
or TabHost
and others, maybe the xxxActivity
is a subActivity
of its parent.对于ActivityGroup
或TabHost
和其他人,也许xxxActivity
是一个subActivity
其父。 Then the startActivityForResult
can not work but the parent can get the result.然后startActivityForResult
无法工作,但父级可以获得结果。
call getParent().startActivityForResult()
from your sub-activity从您的子活动中调用getParent().startActivityForResult()
your parent (the ActivityGroup
) will be able to handle the onActivityResult
.您的父母( ActivityGroup
)将能够处理onActivityResult
。 So I created a subclass of ActivityGroup
and handled this onActivityResult
.所以我创建了一个ActivityGroup
的子类并处理了这个onActivityResult
。
You can re-route that result back to the sub-activity if you need to.如果需要,您可以将该结果重新路由回子活动。 Just get the current activity by getLocalActivityManager().getCurrentActivity()
.只需通过getLocalActivityManager().getCurrentActivity()
获取当前活动。 My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data)
in that subclass for the ActivityGroup
to call.我的子活动继承自自定义活动,因此我在该子类中添加了一个handleActivityResult(requestCode, resultCode, data)
供ActivityGroup
调用。
example: http://www.cnblogs.com/relinson/archive/2012/03/25/startActivityForResult.html例如: http : //www.cnblogs.com/relinson/archive/2012/03/25/startActivityForResult.html
如果您在清单文件中拼错了包或类名, onActivityResult()
也会将RESULT_CANCELED
作为resultCode
传递。
在 Android Manifest 中,为您想要打开的活动设置android:launchMode="singleTop"
并在打开活动时设置标志intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
如果您在 AndroidManifest.xml 的 Activity 中定义了android:noHistory="true"
,则会在此处导致相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.