1

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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) 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.