android - launch an activity of another app to get it's result

judepereira picture judepereira · Apr 25, 2013 · Viewed 7.3k times · Source

I have two applications, A and B.

From A, I'm launching B for a result, using the following code:

Intent fmIntent = getPackageManager().getLaunchIntentForPackage("com.example.B");
fmIntent.putExtra("hello", "world");
startActivityForResult(fmIntent, REQUEST_TEST);

From B, I'm doing the following:

getIntent().putExtra("completed", true);
setResult(RESULT_OK, getIntent());
finish();

If I do the above for an activity within the same app, it works as expected.

However, since its two different apps, I receive an empty intent with no data and an unset result code. How should I edit the above to ensure that one intent is maintained throughout?

Answer

Ayaz Alifov picture Ayaz Alifov · Nov 30, 2015

Use setFlags(0) to clean all flags which can be created by getLaunchIntentForPackage:

Intent fmIntent = getPackageManager().getLaunchIntentForPackage("com.example.B");
fmIntent.setFlags(0);
fmIntent.putExtra("hello", "world");
startActivityForResult(fmIntent, REQUEST_TEST);