Rama Bramantara Rama Bramantara - 9 days ago 7
Android Question

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();
}
}

Answer

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