2

I need to pass object user to another activity. I know I should use Parcelable, but I can't edit User class, because it is used from maven repository.

Is it here any other way how can I pass user object? Or how to locally edit class from maven repository?

Sk1X1
  • 1,305
  • 5
  • 22
  • 50
  • You can probably use a event bus with sticky event – rahul.ramanujam Jun 10 '15 at 23:31
  • I think you should read all users in an arraylist, Create a variable of user type, get/find/match the user object that you want to pass to another activity (best with for loop) with the items in arraylist , once got/found/matched the user - assign the value to the variable that is created above, and then you can call any method and pass the variable as a parameter to another activity. – VD007 Jun 10 '15 at 23:48

3 Answers3

4

You can use the Application class for this purposes. what you need is:

  1. create a class which derives from Application.
  2. declare the class in the AndroidManifest.xml (see here)
  3. create a object variable of type User in it (also getter and setter).
  4. assign the user object in first activit through:

    MyApplication app = (MyApplication) getApplication(); app.setUser(user);

  5. retrieve the object in other activity through:

    MyApplication app = (MyApplication) getApplication(); User user = app.getUser();

BUT: take care, after you restart you app (go in background and open it again), a new Application object will be created, where the User is null, take care of that then.

This problem can be read here.

Hope this helps.

Community
  • 1
  • 1
Paul Reznik
  • 965
  • 1
  • 6
  • 18
2

You can serialize the object into some kind of string representation. The easiest way to do this is to convert the object into a JSON string, pass the string to the other activity via intent and convert the string back into the original object.

Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
0

What I am doing is just converting the object into Json and then sending as a String using Intent. Then you can't catch it and parse Json.

P.S. Using google's Gson class making this very easy. https://code.google.com/p/google-gson/

Vladislav Kan
  • 354
  • 1
  • 12