I have two objects instantiated from two different class and both classes do not implement parcelable nor serializable. and I want to pass those objects to another activity, so I wrote the below code:
*code:
//send object
Intent intConnect = new Intent(mCtx.getApplicationContext(), ActConnect.class);
Bundle bndConnect = new Bundle();
bndConnect.putParcelable("HeaderModel", (Parcelable) mHeaderModel);
bndConnect.putParcelable("DetailsModel", (Parcelable) mDetailsModel);
intConnect.putExtras(bndConnect);
intConnect.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mCtx.startActivity(intConnect);
//receive objects in the receiving activity
Bundle extras = getIntent().getExtras();
Header headerModel = (Header) extras.get("HeaderModel");
Details detailsModel = (Details) extras.get("DetailsModel");
but at run time, I receive the below logcat:
logcat:
10-08 11:55:44.225 13138-13138/com.example.com.bt_11 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.com.bt_11, PID: 13138
java.lang.ClassCastException: com.example.com.adapter.Header cannot be cast to android.os.Parcelable
at com.example.com.adapter.MyExpandableList$1.onClick(MyExpandableList.java:152)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20893)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
how can I pass non parcelable objects to from activity to another activity?
You can do this way:
Your Model class will looks like below:
public class ModelClass implements Serializable{
// Other stuff
}
How to pass:
Intent mIntent = new Intent(mContext, NextActivity.class);
mIntent.putExtra("HeaderModel", mHeaderModel);
startActivity(mIntent);
How to get:
Header headerModel = (Header) getIntent.getSerializableExtra("HeaderModel");
Hope this will help you.