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

2

13

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?

Motorcade answered 25/4, 2013 at 19:2 Comment(9)
Have you followed the guide here: developer.android.com/training/basics/intents/filters.html ?Chumley
What makes you think that getLaunchIntentForPackage() would ever return something suitable for use by startActivityForResult()?Drome
I just went by guessing it would.Motorcade
Let me take a look at the intent filters.Motorcade
I figured it out from #15205077Motorcade
Did you can solve this problem question ? Now I have problem the same like this your question.if you have solve this question pls share your code.ThanksEquitation
@MinTheinWin I used deep links for this. This looks like an android limitation.Motorcade
@Motorcade What you mean? You can't help me?Equitation
No I can't. The way around this is to use a deep link.Motorcade
F
11

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);
Fracture answered 30/11, 2015 at 11:28 Comment(0)
M
5

The solution is provided in a related question "Android onActivityResult triggered before activity even starts!". Create the intent this way:

Intent myIntent = new Intent();
myIntent.setClassName("com.example.B", "com.example.B.ActivityB");
startActivityForResult(myIntent, 600);

I was facing the same problem and solved this way.

Machine answered 8/2, 2015 at 23:51 Comment(1)
How do you get the result from it?Cecilececiley

© 2022 - 2024 — McMap. All rights reserved.