Search code examples
javaandroidoopobjectserializable

Passing an object from the 2nd activity back to main activity using serializable in android


The first block of code below is my main activity in which I created the intent to the second activity. On this activity I am displaying the expense in a list view which for now I have left out as it is not fully implemented. What I simple want to do is launch the second activity and let the user enter in details and press a button to add the activity to the list view. @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId();

        if (id == R.id.addExpense) {
            Intent intent = new Intent(this, ExpenseActivity.class);
            startActivityForResult(intent, 1);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // check that it is the SecondActivity with an OK result
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Expense expense = (Expense) data.getSerializableExtra("sampleObject");
            Expenses.add(expense);
        }
    }
}


 final Button btnAddExpense = (Button) findViewById(R.id.btnAddExpense);
    btnAddExpense.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           String amountV = txtAmountVat.getText().toString();
           int amountVTwo = Integer.parseInt(amountV);
           String amountI = txtAmount.getText().toString();
           int amountITwo = Integer.parseInt(amountI);

            Expense expense = new Expense(amountITwo, amountVTwo, txtDateOfExpense.getText().toString(), txtDateAdded.getText().toString(), datePaid, paid, txtDes.getText().toString(), imageUri );

            Intent intent = new Intent();
            intent.putExtra("Expense", expense);
            setResult(MainActivity.RESULT_OK, intent);
            finish();
        }
        });

And this is my second activity in which the user enters in data. When i try pass back the expense object the emulator states the app has stopped working. Please could I have some help as I don't know what is causing this problem. This is what my class looks like.

public class Expense implements Serializable {

private int _amount, _amountVat;
private String _dateOfExpense, _dateAdded, _datePaid, _expenseDescription;
private Boolean _paid;
private Uri _imageUri;

public Expense(int amount, int amountVat, String dateOfExpense, String dateAdded, String datePaid, Boolean paid, String expenseDescription, Uri imageUri){

    _amount = amount;
    _amountVat = amountVat;
    _dateOfExpense = dateOfExpense;
    _dateAdded = dateAdded;
    _datePaid = datePaid;
    _paid = paid;
    _expenseDescription = expenseDescription;
    _imageUri = imageUri;
}

public int get_amount() {
    return _amount;
}

public void set_amount(int _amount) {
    this._amount = _amount;
}

public int get_amountVat() {
    return _amountVat;
}

public void set_amountVat(int _amountVat) {
    this._amountVat = _amountVat;
}

public String get_dateOfExpense() {
    return _dateOfExpense;
}

public void set_dateOfExpense(String _dateOfExpense) {
    this._dateOfExpense = _dateOfExpense;
}

public String get_dateAdded() {
    return _dateAdded;
}

public void set_dateAdded(String _dateAdded) {
    this._dateAdded = _dateAdded;
}

public String get_datePaid() {
    return _datePaid;
}

public void set_datePaid(String _datePaid) {
    this._datePaid = _datePaid;
}

public Boolean get_paid() {
    return _paid;
}

public void set_paid(Boolean _paid) {
    this._paid = _paid;
}

public Uri get_imageUri() {
    return _imageUri;
}

public void set_imageUri(Uri _imageUri) {
    this._imageUri = _imageUri;
}

public String get_expenseDescription() {return _expenseDescription;}

public void set_expenseDescription(String _expenseDescription) {this._expenseDescription = _expenseDescription;}

}


Solution

  • Much can't be said about your problem without proper log details. But you can go through these points. The problem with Serializable approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. So, it might be due to this. Try running on a real device & see if it persists. Alternatively, you can implement Parcelable to your class which is faster than Serializable.