Serializable object in intent returning as String
Posted
by B_
on Stack Overflow
See other posts from Stack Overflow
or by B_
Published on 2010-06-16T18:26:52Z
Indexed on
2010/06/16
21:22 UTC
Read the original article
Hit count: 247
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;
...
}
© Stack Overflow or respective owner