How to get startActivityForResult on external activity to work?

A search for high and low values โ€‹โ€‹yielded no results for my problem. Therefore, I finally send a complaint for help.

I have two applications, both written by me. Appendix A launches Appendix B by passing parameters through Intent.putExtra (). This works great when starting application B, the parameters are passed beautifully.

However, I cannot find a way to return the response to app A. Using startActivityForResult () always gave me an immediate onActivityResult () with RESULT_CANCELED. Upon further inspection, the logarithm gave me a warning saying: "Activity starts as a new task, therefore canceling the result of the activity."

I tried to make Activity App B with a different launch mode, action filters (android.intent.action.PICK), but didn't change anything.

Am I trying to do the impossible? From what I understand, what I'm trying to do should be like using third-party actions to select photos from the deviceโ€™s photo gallery.

EDIT:

Ok, I tried to remove the LAUNCHER category from Activity B, but it still does not work. Here is my activity:

<activity android:name=".${CLASSNAME}" android:label="@string/app_name" android:configChanges="mcc|mnc|locale|keyboardHidden|orientation" android:launchMode="standard"> <intent-filter> <action android:name="android.intent.action.PICK" /> </intent-filter> </activity> 

Has anyone really got this to work? I am starting to suspect that starting another application will never be able to return results, since it seems that it will always start a new task no matter what you put in the "intent filter".

+5
source share
3 answers

Make sure that the program you are running does not have android: launchMMM installed on it in the manifest, and check the android: taskAffinity is not used. See here:

http://developer.android.com/guide/topics/manifest/activity-element.html#aff

Make sure that the Intent that you use to start the activity does not have FLAG_ACTIVITY_NEW_TASK installed on it. See here:

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

In particular, the note: "This flag cannot be used when the caller requests the result from the start of activity."

If the action starts as part of a new task, then Android will immediately call onActivityResult () with RESULT_CANCELED, because the action in one task cannot return the results to another task, only actions in the same task can do this.

+9
source

Having the same problem, I took a look at the source code and why the NEW_TASK flag was added.

It turns off if either the activity of source A or the target activity of B is used in the single-instance launch mode, the NEW_TASK flag is automatically added:

  if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) { // The original activity who is starting us is running as a single // instance... this new activity it is starting must go on its // own task. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) { // The activity being started is a single instance... it always // gets launched into its own task. launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK; } 

Since you have both applications, you must be sure that these launch modes are not defined in the manifest or intent.

So far, I could not find any other instance of the NEW_TASK flag set reluctantly.

+1
source

In your activity B you should have something like this,

 Intent intent = new Intent(); setResult(Activity.RESULT_OK, intent); finish(); 

or maybe,

 setResult(Activity.RESULT_OK); finish(); 

where you do not need to transfer any data to action A.

Otherwise, it will always end with the result code Activity.RESULT_CANCELED ;

If the child activity for some reason does not stop (for example, a failure), the parent activity will receive a result with the code RESULT_CANCELED.

Hope this helps.

0
source

Source: https://habr.com/ru/post/954443/


All Articles