Rama Bramantara
Rama Bramantara

Reputation: 76

startActivityForResult() immediately call OnActivityResult when call another app

I have 2 app, A and B.

in app A, I want to call app B and get result from app B. I try this Code:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.app.B");
startActivityForResult(launchIntent, CODE);

but after I called that code, the onActivityResult() method called immediately and give result RESULT_CANCELLED.

App B manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.B">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

App B TestActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    if (getCallingActivity() == null) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    } else {
        Intent returnIntent = new Intent();
        setResult(RESULT_OK, returnIntent);
        finish();
    }
}

Upvotes: 1

Views: 1458

Answers (3)

Senthil Vidhiyakar
Senthil Vidhiyakar

Reputation: 182

That is what you have coded for.. Once the Activity of B app is launched you have called the setResult method because it is in the onCreate() method.. The reason you are getting failed result code is because you have set

setResult(RESULT_OK, returnIntent)

but you should have used Activity.RESULT_OK like

setResult(Activity.RESULT_OK, returnIntent)

Upvotes: 1

Rama Bramantara
Rama Bramantara

Reputation: 76

I just add launchIntent.setFlags(0); before startActivityForResult() based on this answer

Upvotes: 3

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10871

getCallingActivity()

woun't be null only in case if you would call the specific Activity with your startActivityForResult Intent.
But you are calling not the specific activity, but getPackageManager().getLaunchIntentForPackage("com.app.B");
which retrieves the default launch activity of the specified package, and starts it with default(empty) intent.

Upvotes: 0

Related Questions