0

I have 2 Activity, I want to send data from activity 1 to Activity 2 when clicking a list item. On clicking i can get the data mapped to that ListItem, that data is a kind of RealmList type. I used Gson library to convert my model to Json String. I can achieve this if my data is a type of List but am using RealmList. When i try to Achieve the following it my application gets freezed.

This is my filter Model :

    Filter filter = new Filter();
    filter.setTitle("");
    filter.setId("");
    filter.setCode("");

RealmList is like :

    RealmList<Filter> list = new RealmList<Filter>();

I tried to convert above list to JSONObject string as follows :

 new Gson().toJson(filter))

Above live giving me the following error :

 PID: 12959 Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago.  Wait queue length: 4.  Wait queue head age: 5647.8ms.)
                                            Load: 10.67 / 9.63 / 9.38
                                            CPU usage from 1103ms to -6084ms ago:
                                              101% 12959/com.anubavam.creatrix: 98% user + 3.4% kernel / faults: 6335 minor 15 major
                                              44% 832/system_server: 25% user + 18% kernel / faults: 8128 minor 104 major
                                              10% 1623/com.android.phone: 5.5% user + 5.4% kernel / faults: 4275 minor 57 major
                                              3.6% 4304/android.process.media: 1.7% user + 1.9% kernel / faults: 4931 minor 73 major
                                              0% 286/debuggerd: 0% user + 0% kernel / faults: 3205 minor 38 major
                                              5.8% 91/kswapd0: 0% user + 5.8% kernel
                                              5.8% 1272/com.android.systemui: 3.8% user + 1.9% kernel / faults: 2852 minor 39 major
                                              4.5% 302/zygote: 3% user + 1.5% kernel / faults: 11239 minor
                                              3.4% 1580/com.motorola.process.system: 2.3% user + 1.1% kernel / faults: 1881 minor 1 major
                                              3.3% 153/mmcqd/0: 0% user + 3.3% kernel

Note List and ArrayList does not give me any error, its working file.But RealmList is the problem. I tries following line also, but no use.

    List<Filter> list = new RealmList<Filter>();

can any one have work around for this.

Anbarasu Chinna
  • 975
  • 9
  • 28

3 Answers3

2

As of now, Realm objects are not Serializable, See this answer. That means you cannot pass this object through Intent to other activity or converting it to JSON using GSON library.

However, there are two ways using which you can have your model in your second activity.

  1. Pass the primary key of this model through Intent and in second
    activity fetch the model again from DB using this key.
  2. You can this Parceler library for serializing the object and passing to second activity. Check this answer for using Parceler.
Community
  • 1
  • 1
Faraz
  • 2,144
  • 1
  • 18
  • 28
1

Realm currently does not support passing data through Intent. Options available are

1) Send some unique Id to second Activity and requery for the same object on the second Activity

2) Also by using Third party Parceler

You may need to add the dependencies as well

compile "org.parceler:parceler-api:x.x.x"
apt "org.parceler:parceler:x.x.x"
Sreehari
  • 5,621
  • 2
  • 25
  • 59
1

I want to send data from activity 1 to Activity 2 when clicking a list item.

Parcelling data and creating unmanaged/detached copies of data is a misuse of Realm.

Send the primary key of the data through the intent, and requery the object in the second activity.

Intent intent = new Intent(this, OtherActvity.class);
intent.putExtra("itemId", listItem.getId());
startActivity(intent);

public class OtherActivity extends AppCompatActivity {
    Realm realm;

    ListItem listItem;

    @Override
    public void onCreate(Bundle bundle) {
        realm = Realm.getDefaultInstance();
        super.onCreate(bundle);
        listItem = realm.where(ListItem.class).equalTo("id", getIntent().getExtras().getString("itemId")).findFirst(); // re-query
        // ...
    }

    @Override
    public void onDestroy() {
        realm.close();
        realm = null;
        super.onDestroy();
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428