Android – how do I pass non Parcelable objects from one activity to another?
•
Android
I have two objects instantiated from two different classes, which do not implement parseable or serializable. I want TP to pass these objects to another activity, so I wrote the following 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");
However, when running, I receive the following logcat:
Of 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 do I pass non assignable objects from an activity to another activity?
resolvent:
You can do this:
Your model class is as follows:
public class ModelClass implements Serializable{
// Other stuff
}
How to:
Intent mIntent = new Intent(mContext, NextActivity.class);
mIntent.putExtra("HeaderModel", mHeaderModel);
startActivity(mIntent);
How to get:
Header headerModel = (Header) getIntent.getSerializableExtra("HeaderModel");
I hope this will help you
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码