1

I can't pass a matrix of integers between two activities. here is the code:

  • Activity A:

    intent.putExtra("matrix_", (Serializable)matrix);

  • Activity B:

    Bundle extras = getIntent().getExtras();
    matrix =  (int[][]) extras.getSerializable("matrix_");
    
Cœur
  • 37,241
  • 25
  • 195
  • 267
bisemanu
  • 441
  • 2
  • 9
  • 19

2 Answers2

8

There is a simple way to pass matrix through intent.

Activity A:

float[] values = new float[9];
matrix.getValues(values);
intent.putExtra("matrix_values", values);

Activity B:

float[] values = getIntent().getFloatArrayExtra("matrix_values");
Matrix matrix = new Matrix();
matrix.setValues(values);
Peter Zhao
  • 7,456
  • 3
  • 21
  • 22
0

When you are creating an object of intent, you can take advantage of following two methods for passing objects between two activities.

putParceble

putSerializable

What you can do with this, is have your class implement either Parcelable or Serializable.

Then you can pass around your custom classes across activities. I have found this very useful.

Here is a small snippet of code I am using

Matrix matrix  = new Matrix ();
Intent i = new Intent();

Bundle b = new Bundle();
b.putParcelable("CUSTOM_LISTING", matrix  );
i.putExtras(b);
i.setClass(this, NextActivity.class);
startActivity(i);

And in newly started activity code will be something like this...

Bundle b = this.getIntent().getExtras();
if(b!=null)
    mCurrentListing = b.getParcelable("CUSTOM_LISTING");

** EDITED WITH LINKS::: **

LINK1 consist of sample code

LINK2

LINK3

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • i apologize because I'm still a beginner with programming, i cannot understand how to use your advice. My application, in the first activity, takes as input two matrix and performs the multiplication. in the second activity takes the result of the multiplication from the first activity and displays it – bisemanu Apr 21 '12 at 08:45
  • 1
    Seems like Matrix is not parcelable. William's answer worked for me – S B Dec 27 '14 at 10:49
  • This should not be the accepted answer as mentioned above. The links given are for extended subclasses that actually implement `Parcelable`, and `Matrix` clearly isn't one of them. – Allan W Aug 27 '17 at 22:37