Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - How to get startActivityForResult on external Activity to work?

Searching high and low has yielded no result for my problem. Hence I'm finally posting to plead for some assistance.

I have two app, both written by me. App A launches App B, passing in parameters through Intent.putExtra(). This works perfectly fine when App B is launched the parameters are passed nicely.

However, I can't find a way to return a response to App A. Using startActivityForResult() always gave me immediate onActivityResult() with RESULT_CANCELED. Upon further inspection, the logcat gave me a warning stating "Activity is launching as a new task, so cancelling activity result".

I tried making Activity of App B with different launch mode, action filters (android.intent.action.PICK), but nothing I do changed anything.

Am I trying to do the impossible? From what I understand, what I am attempting to do should be similar to using third-party activity to pick pictures from the device's photo gallery.

EDIT:

Ok, I tried removing the LAUNCHER category from Activity B but it still doesn't work. Here's 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 anybody actually got this to work? I'm beginning to suspect starting an activity that is of another app can never return results since it seems it'll always start a new task no matter what you put in the "intent-filter".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure that the Activity you are launching doesn't have android:launchMode set on it in the manifest and check the android:taskAffinity is not being used. See here:

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

Make sure that the Intent you are using to launch the activity doesn't have FLAG_ACTIVITY_NEW_TASK set on it. See here:

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

In particular note: "This flag can not be used when the caller is requesting a result from the activity being launched."

If the activity is being launched as part of a new task then Android will immediately call the onActivityResult() with RESULT_CANCELED because an activity in one task can't return results to another task, only activities in the same task can do so.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...