0

I have an object called Contact which contains an arraylist of type Transactions.

I pass the selected contact (from a listview in activity1) to activity2 using intent and startActivityForResult.

In the second activity I create a new Transaction object and add it to the passed Contact's arraylist of transactions, return it using setResult and retrieve it using onActivityResult

My problem is that the original Contact object doesn't get the new Transaction.

Sending intent:

Intent intent = new Intent(this, AddTransactionActivity.class);
        intent.putExtra("contact", selectedContact);
        startActivityForResult(intent, 1);

recieving intent:

Bundle b = getIntent().getExtras();

    if(b != null) {
        recievedContact = (Contact)b.getParcelable("contact");
    }

Sending back result:

recievedContact.addTransaction(transaction);

    Intent intent = new Intent(this, Contacts_activity.class);
    intent.putExtra("contact", recievedContact);
    setResult(Activity.RESULT_OK, intent);
    finish();
    startActivity(intent);

Retrieving result:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1) {
        if(resultCode == Activity.RESULT_OK) {
            Bundle b = data.getExtras();
            if(b != null) {
                selectedContact = (Contact)b.getParcelable("contact");
            }
        } else if (resultCode == 0) {
        }
    }
}

edit: I put a Log.v("result test", "success"); in onActivityResult(), it doesnt show in logcat so it seems my onActivityResult method is never called.

  • This might help you, http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android –  Oct 08 '14 at 14:01
  • @Mohan well i am able to pass an object with intent, the thing im trying to do is make changes to the object in another activity, so that the object in the first activity updates with the changes – user2798413 Oct 08 '14 at 14:07
  • remove startActivity(intent); in the AddTransactionActivity class after finish(); It works – Harsha Vardhan Oct 08 '14 at 15:17
  • Ran into a new problem, when i use an == check in onActivityResult to see if the returned object == the object i first passed with the intent it returns false. – user2798413 Oct 08 '14 at 16:46
  • You won't get the same object back. You will get a completely new object. So you'll have to either replace the original object with the one returned in `onActivityResult()` or you need to copy what you want out of the one and into the other. When you add an EXTRA to an `Intent`, the object is serialized. It doesn't actually pass the real object to the new Activity. It just passes a serialized version of it. – David Wasser Oct 09 '14 at 08:56
  • @DavidWasser thank you, i ended up just returning a transaction object and putting it in the selected contact :) – user2798413 Oct 09 '14 at 14:30

0 Answers0