5

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 1
    i think that pass object from one activity to another class must implement parcelable or serializable – USKMobility Oct 08 '15 at 10:41
  • Could you convert your object to Json, store in Shared Preferences, and retrieve using Gson? http://stackoverflow.com/questions/5571092/convert-object-to-json-in-android http://stackoverflow.com/questions/14368624/json-to-java-object-using-gson I have done this a couple of times, however I don't know how its performance compares to normal methods of Parcelable and Serializable. – Mark Oct 08 '15 at 10:43
  • 1
    If you can modifiy that class i will recommend that you change it to implement Serializable. – Nanoc Oct 08 '15 at 10:46

4 Answers4

3

If your class doesn't implement parcelable nor serializable, and you cannot modify them (code not under your control perhaps), then you have no way to directly send the data between the two activities.

However, you can pass the data indirectly between the two activities. You could store them in a singleton class (however singletons are hard to test etc.), you could save and retrieve them off your application class, or you could persist them into sharedpreferences, a file or a database to be loaded by the second activity.

Jon Finerty
  • 198
  • 1
  • 8
  • Indeed, if you really, really need to do this, just use a Singleton Storage on the MainApp and be done with it. As I get older I learn the value of pragmatism. People just overreact to antipatterns. Know what you are doing, learn the pros and cons, and make an informed choice. Yes, using the Singleton, might force you to implement a method like `clear` and use the teardown method of your unit test framework carefully, but the opposite can result in a cumbersome design with a lot of work hours designed to it. Remember, the most expensive resource is your time. – El Marce Mar 15 '22 at 23:18
1

The best way to pass object without parcelable or serializable is to use Gson Lib

And use this extensions:

fun Intent.putObject(name: String, value: Any) {
    val jsonValue = Gson().toJson(value)
    this.putExtra(name, jsonValue)
}

inline fun <reified T : Any> Intent.getObjectExtra(name: String): T? {
    val json = this.getStringExtra(name)

    val obj = Gson().fromJson<T>(json, T::class.java)
    return (obj as T)
}

inline fun <reified T : Any> Bundle.getObject(name: String): T? {
    val json = this.getString(name)

    val obj = Gson().fromJson<T>(json, T::class.java)
    return (obj as T)
}

Using:

val intent = Intent(this,YourActivity::class.java)
intent.putObject(name = "myKey", value = MyObject)

//To get the object in the other activity
val product = intent.extras?.getObject<MyObject>(name = "myKey")
// or
val product = intent.getObjectExtra<MyObject>(name = "myKey")
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46
-2

try like this:

     Bundle bundle=getIntent().getExtras();

    Header headerModel =(Header)) bundle.getParcelable("HeaderModel");
Vitaliy Ptitsin
  • 329
  • 1
  • 5
  • 14
-3

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.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151