1

I want to pass RealmObject using intent and i was doing from Serilizable but it throwing me java.lang.ClassCastException: io.realm.RealmResults cannot be cast to java.io.Serializable

// else method is running and throwing Exception // Value of flag is 1.

  lay_upcoming.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.i("flag", String.valueOf(flag));
            Log.i("flag", String.valueOf(img_id));
            Log.i("flag", String.valueOf(flag));

            if (flag == 0) {
                bundle = new Bundle();
                eventFragment = new EventFragment();
                bundle.putSerializable("Data", (Serializable) data2);
                bundle.putSerializable("CustomerData", (Serializable) customerData);
                bundle.putInt("img_id", img_id2);
                eventFragment.setArguments(bundle);

                getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up).addToBackStack(null).replace(R.id.fragment_container, eventFragment).commit();

            } else {
                bundle = new Bundle();
                eventFragment = new EventFragment();
                bundle.putSerializable("Data", (Serializable) allEventModels);
                bundle.putSerializable("CustomerData", (Serializable) customerDataModels);
                bundle.putInt("img_id", img_id);
                eventFragment.setArguments(bundle);

                getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up).addToBackStack(null).replace(R.id.fragment_container, eventFragment).commit();

            }
        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You could theoretically `copyFromRealm` then convert to JSON with GSON, then send it as a string. But it will become a detached object, which may or may not be what you need. – EpicPandaForce Jun 12 '19 at 12:52

2 Answers2

4

You're not going to be able to pass a Realm object using intents, parcelables etc. Your solution here is to pass an identifier and let the destination activity query the Realm itself and retrieve the object.

You haven't included the class definition in the question, so I can't give you a complete answer. But I would assume you have either a primary key defined or some other unique id/combination of ids that you could pass as strings or ints to the intent, and then retrieve the object via query.

Chris Shaw
  • 1,610
  • 2
  • 10
  • 14
0

If you want to pass a realm object between two component through intent You can make use of,

  1. Gson for Serialization pass it using intent.putString()

  2. You can make implement Parcelable to your realm object and pass it using Bundle

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable("YOUR_OBJECT_KEY",YOUR_OBJECT);
        intent.putExtras(bundle);
    

    And retrieve like

    getIntent().getExtras().getParcelable("YOUR_OBJECT_KEY")
    
user3135923
  • 61
  • 1
  • 6
  • 1
    Chris Shaw solution is better for performance and usage. I would not recommend to do as you suggest – Maelig Jun 13 '19 at 12:57