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;