Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializable object in intent returning as String

In my application, I am trying to pass a serializable object through an intent to another activity. The intent is not entirely created by me, it is created and passed through a search suggestion.

In the content provider for the search suggestion, the object is created and placed in the SUGGEST_COLUMN_INTENT_EXTRA_DATA column of the MatrixCursor. However, when in the receiving activity I call getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY), the returned object is of type String and I cannot cast it into the original object class.

I tried making a parcelable wrapper for my object that calls out.writeSerializable(...) and use that instead but the same thing happened.

The string that is returned is like a generic Object toString(), i.e. com.foo.yak.MyAwesomeClass@4350058, so I'm assuming that toString() is being called somewhere where I have no control.

Hopefully I'm just missing something simple. Thanks for the help!

Edit: Some of my code

This is in the content provider that acts as the search authority:

//These are the search suggestion columns 
private static final String[] COLUMNS = {
    "_id",  // mandatory column
    SearchManager.SUGGEST_COLUMN_TEXT_1,
    SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
};

//This places the serializable or parcelable object (and other info) into the search suggestion
private Cursor getSuggestions(String query, String[] projection) {
    List<Widget> widgets = WidgetLoader.getMatches(query);

    MatrixCursor cursor = new MatrixCursor(COLUMNS);
    for (Widget w : widgets) {
        cursor.addRow(new Object[] {
                           w.id
                           w.name
                           w.data //This is the MyAwesomeClass object I'm trying to pass
                           });
    }

    return cursor;
}

This is in the activity that receives the search suggestion:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Object extra = getIntent().getSerializableExtra(SearchManager.EXTRA_DATA_KEY); 
    //extra.getClass() returns String, when it should return MyAwesomeClass, so this next line throws a ClassCastException and causes a crash
    MyAwesomeClass mac = (MyAwesomeClass)extra;
    ...
 }
like image 348
B_. Avatar asked Jun 16 '10 18:06

B_.


1 Answers

Read my answer to a similar question. The basic problem is that the MatrixCursor only works for base types and depends on the AbstractCursor to fill the CursorWindow to pass the data between processes. AbstractCursor does this by calling Object#toString on every row data field. In other words, you cannot pass arbitrary objects between processes via a MatrixCursor.

like image 89
Rich Schuler Avatar answered Nov 09 '22 22:11

Rich Schuler