简体   繁体   English

Android - startActivityForResult 立即触发 onActivityResult

[英]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 .如果您的活动作为singleInstancesingleTask启动,则不能使用startActivityForResult() standard or singleTop launch mode will fix the problem. standard或单singleTop启动模式将解决问题。

Additionally make sure the intent does not have the Intent.FLAG_ACTIVITY_NEW_TASK set.另外确保意图没有设置Intent.FLAG_ACTIVITY_NEW_TASK

From the docs :文档

This flag can not be used when the caller is requesting a result from the activity being launched.当调用者从正在启动的活动中请求结果时,不能使用此标志。

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 棒棒糖前版本中的singleInstancesingleTask活动。 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 有一个关于在活动关闭结束时等待返回的小问题要解决此行为,重要的是设置:

  • all activities will have the same task Affinity attribute.所有活动都将具有相同的任务关联属性。 ( 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.对于ActivityGroupTabHost和其他人,也许xxxActivity是一个subActivity其父。 Then the startActivityForResult can not work but the parent can get the result.然后startActivityForResult无法工作,但父级可以获得结果。

  1. call getParent().startActivityForResult() from your sub-activity从您的子活动中调用getParent().startActivityForResult()

  2. 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

  3. 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.

相关问题 startActivityForResult不触发onActivityResult - startActivityForResult not triggering onActivityResult Android:onActivityResult()未调用startActivityForResult - Android: onActivityResult() is not calling for startActivityForResult Android:startActivityForResult没有调用onActivityResult - Android: startActivityForResult not calling onActivityResult Android startActivityForResult,setResult,onActivityResult未调用 - Android startActivityForResult, setResult, onActivityResult not called Android:startActivityForResult停止调用onActivityResult - Android: startActivityForResult stopped calling onActivityResult startActivityForResult创建立即触发的待处理通知 - startActivityForResult to create pending notification triggering immediately 在 startActivityForResult 后立即调用 Cordova onActivityResult 并带有 RESULT_CANCELED - Cordova onActivityResult is called immediately after startActivityForResult with RESULT_CANCELED 当调用另一个应用程序时,startActivityForResult()立即调用OnActivityResult - startActivityForResult() immediately call OnActivityResult when call another app ACTION_APPLICATION_DETAILS_SETTINGS的startActivityForResult不触发onActivityResult - startActivityForResult for ACTION_APPLICATION_DETAILS_SETTINGS not triggering onActivityResult Android startActivityForResult和onActivityResult具有不可预测的行为 - Android startActivityForResult and onActivityResult has unpredictable behaviours
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM