I have an ArrayList of objects that I save in SharedPreferences in my onPause method. When I try to serialize it in json, I get the following in the logs (these two statements repeat and overload logcat):
06-20 20:33:31.620 26245-26252/com.example.app W/art﹕ Suspending all threads took: 21.556ms 06-20 20:33:31.620 26245-26260/com.example.app W/art﹕ Suspending all threads took: 5.901ms 06-20 20:33:31.650 26245-26260/com.example.app I/art﹕ Background partial concurrent mark sweep GC freed 210493(6MB) AllocSpace objects, 87(2MB) LOS objects, 25% free, 47MB/63MB, paused 16.970ms total 155.761ms 06-20 20:33:32.480 26245-26260/com.example.app I/art﹕ Background sticky concurrent mark sweep GC freed 346396(10MB) AllocSpace objects, 140(4MB) LOS objects, 14% free, 48MB/56MB, paused 13ms total 88.199ms
I initialize the ArrayList to onCreate and then pass the objects to it when I finish Asynctask execution. Here is the problem method that causes the user interface to freeze:
@Override protected void onPause() { super.onPause(); String json = mGson.toJson(mSelectedContactList); mSharedPreferences.edit().putString("contact_list", json).apply(); }
I also tried the following and keep on freezing:
JsonElement element = mGson.toJsonTree(mSelectedContactList, new TypeToken<ArrayList<ContactObject>>() { }.getType()); String jsonString = element.getAsJsonArray().getAsString();
I know that the problem is not with SharedPreferences. I suspect the toJson method cannot handle this process, but I cannot figure out what the problem is. Any help would be much appreciated.
* EDIT: Here is the class I'm using:
public class ContactObject implements Parcelable {
source share