1

I read quite some posts here, but none of the solutions I found seems to work for me. I'm afraid I miss something hopefully obvious.

I have two Android applications. I can modify the code of both. I want to send a start application intent (with an extra param) from application1 to application2 and when application2 finished its work it should send back a result intent to application1.

As far as I understood the doc:

Application1

  • should use startActivityForResult()
  • implement onActivityResult, which can then
  • handle the resultCode and the data. at reception

Application2

  • should read the intent with getIntent()
  • create a response intent
  • call setResult()
  • call finish()

I just can't get it to work: application1 always reports, that the activity was cancelled.

Does anyone have a very simple example app that shows how sending / receiving intents is working?

Below some code snippets showing what I tried:

Application1

myintent = getPackageManager().getLaunchIntentForPackage(APPLICATION2NAME);
myintent.putExtra(MY_EXTRA_PARAM_NAME, "avalue");
startActivityForResult(myintent, AN_ID);

and here the handler

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, String.format("got result %d for %d ",
        resultCode, requestCode));
   # here I always receive immediately a result code of 0 
   # (which means if I read the doc correctly ("cancelled")

Application2

Intent intent = getIntent();  # this is working
String param = intent.getStringExtra(MY_EXTRA_PARAM_NAME) ; # successfully received parameter

Intent rslt_int = new Intent(); # create new intent for response
# alternatively I also tried Intent rslt_int = getIntent(); 
# which also fails
# I also tried to reuse the intent with Intent rslt_int = intent;
String rslt = "I confirm the reception of " + param; # just create an answer
rslt_int.putExtra(MY_EXTRA_RSLT_NAME, rslt);
setResult(RESULT_OK, rslt_int); # this does not fail, 
                                # but app1 never sees the result
finish();
return; 
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • visit this [link](http://stackoverflow.com/a/5745291/5255006) – Krunal Kapadiya Nov 29 '16 at 09:12
  • In the second application (which is being started), the activity's entry in the manifest file should contain an intent filter. Is this the issue ? – rhari Nov 29 '16 at 09:15
  • Thanks Krunal, I don't think the intent filter is the issue. I configured it and assume that it is working because application 2 is called and can receive the extra parameter successfully. – gelonida Nov 29 '16 at 18:03

1 Answers1

1

It works if you start the explicit activity for the 2nd application:

Intent myintent = new Intent(Intent.ACTION_MAIN);
                    myintent.setComponent(new ComponentName(APPLICATION2NAME, APPLICATION2NAME +".MainActivity"));
myintent.putExtra(MY_EXTRA_PARAM_NAME, "avalue");
startActivityForResult(myintent, AN_ID);

assuming that the name of your activity is MainActivity. Relace in ur code the starting of Application2 with this.

The difference is that instead of starting the application2 with the package name you are specifying directly the activity to be started from the application2.

petrumo
  • 1,116
  • 9
  • 18
  • This is a workaround, right? you suggest, that application2 just 'starts' application1 and sends its related data. The examples, that I found in the net (but that don't work for me) use all a sequence of creating an intent, calling setResult(RESULT_OK, intent); and calling then finish(); Have to think what potential differenct behaviours would occur with your approach. – gelonida Nov 29 '16 at 18:05
  • it's the fix for the example presented. I explained better – petrumo Nov 29 '16 at 19:02
  • thanks a lot. I understand now your explanation and I had time to test. Yes indeed this solves my problem :-) Somehow I must have overlooked in the documentation (or perhaps it's really mentioned nowhere) , that startActivityForResult() does not work for a launchintent, but only for an explicit intent. Unfortunately I'm a SO newbie and I can't upvote your response. – gelonida Nov 30 '16 at 18:23