Cláudio Rafael
Cláudio Rafael

Reputation: 63

Passing data from one app to another app using onActivityResult

I'm trying to pass data from one app to another app using onActivityResult. Passing Data from 'A' to 'B' is OK. But when I try to return a string from B to A, data.getExtras() always returns null ... Thanks in advance

My code:

In App A:

public void initAppB(Context context, String packageName, String codCli){
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent == null) {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + packageName));
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("codCli",codCli);
    startActivityForResult(intent, 123456);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String reg = "";
    if(requestCode == 123456) {
      if(resultCode == -1) {
        try{
            Bundle MBuddle = data.getExtras(); // >> ALWAYS RETURN NULL <<
            reg = MBuddle.getString("retorno");
        }catch(Exception e){
            log("Error: " + e.getMessage());
        }
        CommitSale(reg);
      } else {
        // error
      }
    }
}

In App B:

....
//It's OK!! Receiving data!
Bundle extras = getIntent().getExtras();
if (extras != null) {
    codCli = extras.getString("codCli");
}

....

OnClickListener mBackListener = new OnClickListener() {
    public void onClick(View v) {
        String registro = "010000";
        Intent intent = getIntent();
        intent.putExtra("retorno",registro);
        setResult(-1, intent); // --> Forcing returning code -1 (Ok)
        finish();
    }
};

Upvotes: 4

Views: 1927

Answers (2)

Ahsan Kamal
Ahsan Kamal

Reputation: 1105

try this: in B:

Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();

And, in A:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode==RESULT_OK && requestCode==1) {
        Bundle mBundle = data.getExtras();
        String mMessage = mBundle.getString("Date");
    }
}

Upvotes: 2

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

Kindly try to understand "what goes around that comes back around" when you are trying to use startActivityForResult()

The problem here is that you are providing a String value in the AppB, like this

intent.putExtra("retorno",registro);

so what you should be reading is a String from the data like this... int AppA's onActivityResult()

data.getStringExtra("retorno");

because you are providing a String so you should expect a String in return,

what your code is trying to do is to read a Bundle in onActivityResult() like this...

Bundle MBuddle = data.getExtras();

which was never provided by the AppB,

If you would like you can have a look at one the example how to use onActivityResult() here Github

Upvotes: 1

Related Questions