1

I have created a custom listener using the following code in EditTransactionActivity:

private OnTransactionUpdateListener mTranUpdateListener;


public interface OnTransactionUpdateListener {
    public void transactionUpdated(Tran tran);
}

public void setTransactionUpdateListener(OnTransactionUpdateListener listener) {
    this.mTranUpdateListener = listener;
}

I call the method using:

mTranUpdateListener.transactionUpdated(mTransaction);

In TransactionDetailActivity I have implemented the class with the following method:

@Override
public void transactionUpdated(Tran tran) {
    this.mTransaction = tran;
}

The problem I have is that the listener has not been set, since I am loading EditTransactionActivity using an Intent, how do I set the listener? I have no instance of the object to set.

Intent intent = new Intent(getApplicationContext(),
                EditTransactionActivity.class);
Bundle bundle = new Bundle(
bundle.putSerializable("transaction_key", mTransaction);
intent.putExtras(bundle);
startActivity(intent);
Carl
  • 841
  • 1
  • 13
  • 33
  • You want communication between two activities if I understand this correctly? – Bojan Kseneman May 06 '15 at 21:08
  • Yes, when I save data in EditTransactionActivity, I call finish() but I want to pass an object back first. – Carl May 06 '15 at 21:30
  • Modify the model solution from the following solution to use your interface. http://stackoverflow.com/questions/19026515/how-to-use-interface-to-communicate-between-two-activities – Bojan Kseneman May 06 '15 at 21:34
  • Basically you would want to make a singleton to hold your interface – Bojan Kseneman May 06 '15 at 21:34
  • There's three classes there though, my interface is within one of my activities. – Carl May 06 '15 at 21:46
  • Yes. You can put your interface in a standalone file if you want and make it public. It doesn't matter. – Bojan Kseneman May 06 '15 at 21:48
  • If I create an instance of the activity and set the listener onCreate, this still doesn't work when the activity is loaded through the intent. – Carl May 06 '15 at 21:57

2 Answers2

1

There are some notes about your code should be correct

1) You can't pass interface (behaviour) directly to bundle as Serializable object, you should pass object of a class, and this class must implement Serializable and OnTransactionUpdateListener interfaces.

2) Even if you do the first note, the listener will not be set, you should manually do it in onCreate method of the activity to get the Serializable object from the extras and update the listener variable.

// psudu code
// in onCreate method of the EditTransactionActivity we will read and set listener
if (getIntent().getExtras() != null &&
    getIntent().getExtras().containsKey("transaction_key")) {

    Serializable serializableObject= getIntent().getExtras().getSerializable("transaction_key");
    if (serializableObjecte instanceof OnTransactionUpdateListener) {
          mTranUpdateListener= (OnTransactionUpdateListener) serializableObject;
    }
}

Note: you can use Parcelable interface instead of using Serializable interface. Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable.

http://developer.android.com/reference/android/os/Parcelable.html

Mu Sa
  • 331
  • 3
  • 8
1

I have solved it passing the Intent back through onActivityResult(). Thanks for the help.

Carl
  • 841
  • 1
  • 13
  • 33