Backendless Support
 
Answered

BackendlessUser as a Parcel

Hi!

After some days looking for a solution, I can't get it working, so here I am.

I'm desperately trying to pass a BackendlessUser as a Parcel from on Activity to another in Android.

Here is my structure:

I have a "Request" class, any user in the app can make a request, and they can make the request for a friend too. So I thought the easiest way to put it into code was to have a relation with the BackendlessUser class.

My request class looks a bit like that:

  1. public class Request implements Parcelable {
  2. private BackendlessUser user;
  3. public BackendlessUser getUser() {
  4. return user;
  5. }
  6. protected Request(Parcel in) {
  7. user = (BackendlessUser) in.readValue(BackendlessUser.class.getClassLoader());
  8. }
  9. @Override
  10. public void writeToParcel(Parcel dest, int flags) {
  11. dest.writeValue(user);
  12. }
  13. }

BUT, it doesn't work! I thought that since BackendlessUser implements Serializable (which is bad, it should be Parcelable), it would work, but it doesn't.

In face, I even tried to simply pass a user as a serializable object in an Intent, aaaand... not better.

  1. Intent intent = new Intent(MainActivity.this, DestinationActivity.class);
  2. intent.putExtra("user", Backendless.UserService.CurrentUser());
  3. startActivity(intent);

Here is what I get:

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.vetasoft.backendlesstest, PID: 7486
  3. java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.backendless.BackendlessUser)
  4. at android.os.Parcel.writeSerializable(Parcel.java:1394)
  5. at android.os.Parcel.writeValue(Parcel.java:1341)
  6. at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
  7. at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
  8. at android.os.Bundle.writeToParcel(Bundle.java:1034)
  9. at android.os.Parcel.writeBundle(Parcel.java:669)
  10. at android.content.Intent.writeToParcel(Intent.java:7511)
  11. at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2440)
  12. at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
  13. at android.app.Activity.startActivityForResult(Activity.java:3843)
  14. at android.app.Activity.startActivityForResult(Activity.java:3804)
  15. at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
  16. at android.app.Activity.startActivity(Activity.java:4114)
  17. at android.app.Activity.startActivity(Activity.java:4082)
  18. at com.vetasoft.backendlesstest.MainActivity$1$1.handleResponse(MainActivity.java:34)
  19. at com.vetasoft.backendlesstest.MainActivity$1$1.handleResponse(MainActivity.java:27)
  20. at com.backendless.UserService$8.handleResponse(UserService.java:764)
  21. at com.backendless.UserService$8.handleResponse(UserService.java:749)
  22. at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
  23. at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
  24. at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
  25. at android.os.Handler.dispatchMessage(Handler.java:98)
  26. at android.os.Looper.loop(Looper.java:135)
  27. at android.app.ActivityThread.main(ActivityThread.java:5300)
  28. at java.lang.reflect.Method.invoke(Native Method)
  29. at java.lang.reflect.Method.invoke(Method.java:372)
  30. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
  31. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
  32. Caused by: java.io.NotSerializableException: java.lang.Object
  33. at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
  34. at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
  35. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
  36. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
  37. at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
  38. at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
  39. at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
  40. at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
  41. at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
  42. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
  43. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
  44. at android.os.Parcel.writeSerializable(Parcel.java:1389)
  45. at android.os.Parcel.writeValue(Parcel.java:1341)
  46. at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
  47. at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
  48. at android.os.Bundle.writeToParcel(Bundle.java:1034)
  49. at android.os.Parcel.writeBundle(Parcel.java:669)
  50. at android.content.Intent.writeToParcel(Intent.java:7511)
  51. at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2440)
  52. at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
  53. at android.app.Activity.startActivityForResult(Activity.java:3843)
  54. at android.app.Activity.startActivityForResult(Activity.java:3804)
  55. at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
  56. at android.app.Activity.startActivity(Activity.java:4114)
  57. at android.app.Activity.startActivity(Activity.java:4082)
  58. at com.vetasoft.backendlesstest.MainActivity$1$1.handleResponse(MainActivity.java:34)
  59. at com.vetasoft.backendlesstest.MainActivity$1$1.handleResponse(MainActivity.java:27)
  60. at com.backendless.UserService$8.handleResponse(UserService.java:764)
  61. at com.backendless.UserService$8.handleResponse(UserService.java:749)
  62. at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
  63. at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
  64. at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
  65. at android.os.Handler.dispatchMessage(Handler.java:98)
  66. at android.os.Looper.loop(Looper.java:135)
  67. at android.app.ActivityThread.main(ActivityThread.java:5300)
  68. at java.lang.reflect.Method.invoke(Native Method)
  69. at java.lang.reflect.Method.invoke(Method.java:372)
  70. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
  71. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Any ideas how I should pass a BackendlessUser from one Activity to another?

Also, using the ownerId is NOT what I need, since the requests are displayed in a List, I need to have every user available (since they are in autoload, it should work).

Best Answer
photo

@Mark I think this is what I'm going to do for now. But it is not a good solution on Android. Parcelable was made especially because Serializable is not optimized. I suggest you talk to the Android Development team at Backendless to see if they could eventually make an Android-Specific Library which implement Parcelable instead of Serializable.

@Jens It may be a solution, but I don't know if Gson can serialize a object like BackendlessUser since all its properties are stored as "Object" in an HashMap.

TL;DR

I made my model implements Serializable AND Parcelable.

But I think i'll switch to the REST api with Retrofit.

ps: I don't know how to change the topic from "Need Answer" to "Answered", the Edit option is not on the first message. Can you switch it for me?

Leave a Comment

Comments (10)

photo
1

Hi Dylan,

How about using the getProperties() method on BackendlessUser to get the data it contains and transfer that between the activities? You can easily transfer the properties out of and into BackendlessUser instance:

https://backendless.com/documentation/users/android/users_user_properties.htm

Regards,

Mark

photo
1

Hi,

HashMap and Map aren't parcelable either. If they contained only primitive values, I could use writeSerializable and write user.getProperties. But my users contains some relations to other objects, that are Parcelable, but not Serializable.

Here is an example:

b2f0651173c485f5e8f8eb61697474c0

In the Request class, I try to write the parcel like this:

  1. public void writeToParcel(Parcel dest, int flags) {
  2. dest.writeSerializable(user.getProperties());
  3. }

But since Address, in the user, is Parcelable, and not Serializable, I get a NotSerializableException:

  1. Caused by: java.io.NotSerializableException: com.vetasoft.mapass.model.Address

Alternatively, I could write every single property of the user in the Parcel, and read them one by one, but this is not really Developer-friendly, and I would have to edit every class of my Model that has a reference to BackendlessUser if I want to add a single property to BackendlessUser...

photo
1

Why parcel/serializing when this can be done with a Singleton?

Have a Singleton, set your value to the singleton in activity a, retrieve it in activity b, done.

photo
1

This is not how you're supposed to pass data between activities.

If the app gets killed by the system, the Singleton will loose its value, whereas I can store a parcelable in case of memory warning.

photo
1

Dylan,

Address is your class, isn't it? Why can't you declare it Parcelable to avoid the NonSerializableException?

Regards,

Mark

photo
1

It is Parcelable, but not Serializable =/

photo
2

Since it is a class you wrote and control, can you make it both? Parcelable and Serializable?

photo
1

I understand the issue with the Singleton vs app lifecycle.

Another option could be to serialize your object into a JSON string (with Gson/Logansquare, sth. else) and put this into your intent as stringExtra. Then in your 2nd activity, parse it back into a class object.

photo
1

@Mark I think this is what I'm going to do for now. But it is not a good solution on Android. Parcelable was made especially because Serializable is not optimized. I suggest you talk to the Android Development team at Backendless to see if they could eventually make an Android-Specific Library which implement Parcelable instead of Serializable.

@Jens It may be a solution, but I don't know if Gson can serialize a object like BackendlessUser since all its properties are stored as "Object" in an HashMap.

TL;DR

I made my model implements Serializable AND Parcelable.

But I think i'll switch to the REST api with Retrofit.

ps: I don't know how to change the topic from "Need Answer" to "Answered", the Edit option is not on the first message. Can you switch it for me?

photo
1

Hi Dylan,

The design constraint we have is keeping the SDK "neutral", meaning it should work both for Android and Java. As a result, any bindings to Android which make core classes unavailable for Java are not desirable.

Regards,

Mark

p.s. I changes the status to Answered.

photo