2

I know there's simple mechanism to pass object between activities. I usually use Bundle and putParcelable() or put Serializable(), but I know that it's actually passing raw data by packing and unpacking objects, without keeping references.

But now my problem is different - I need to pass reference to some services and other activities. Is there any way do do it?


Maybe I explain my issue on real problem:

  1. I have a simple class City, which stores among other things weather data.
  2. I keep it in ListFragment and display it there.
  3. I also have fragment with GoogleMap, which shows Cities on list with weather.

How should I store cities: List<City> in my application?

Kamil Lelonek
  • 14,592
  • 14
  • 66
  • 90

3 Answers3

13

I need to pass reference to some services and other activities

No, you do not. You need to allow "some services and other activities" to have access to the same data. Passing by reference would be one way to achieve this end, but it is an approach not generally used in Android, just as it is not generally used in a Web app (pages do not pass JavaScript objects to other pages, for example).

The typical approach is to have a central data model, such as a singleton managing a bunch of POJOs, backed by a database. Then, you are passing identifiers around in Android, and those identifiers are usually simple values (ints, strings, etc.).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • have you written a blog post or uploaded some code showing this typical approach? – Jose_GD May 31 '13 at 12:47
  • your comparsion is very bad, because any web framework allows easily you pass references between pages through session variable (different frameworks different ways but always possible easily)... and i understand is not your fault android has this terrible archtecture problem (i've no advantage in denying the developer to exchange references between pages, just the disadvantages that everybody says...). and the idea of having a singleton and managing everything from this instance solves the problem but also KILL THE OBJECT ORIENTATION PRINCIPLE... so why to use java? – Rafael Lima May 23 '18 at 02:30
  • @RafaelLima: "because any web framework allows easily you pass references between pages through session variable" -- last I checked, session variables do not allow one Web page to pass a client-side JavaScript object to another Web page's JavaScript object space. That's what I was referring to in my answer. On the server, "any Web framework" uses singletons all over the place (e.g., session pool), though usually with an eye towards hiding the singletons from the individual developer. I suspect that you will see more of that singleton-hiding pattern in Android in the future. – CommonsWare May 23 '18 at 11:19
4

Since the object in question is a simple list, you could extend Application and store the list in that:

public class MyApp extends Application {

    List<City> cityList.....

    //onCreate()

    public List<City> getList() {}

    public List<City> setList()
}

Then simply call:

MyApp app = getApplication(); //getActivity().getApplication() in the Fragment
List<City> mylist = app.getList();
Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

Please see:

Pass Object reference within Intent without implementing Serializable or Parcelable

You cannot pass a reference to an object across process. As Mark points out, you can do a lot of stuff that will be functionally similar. The answer to your question, though, is "no"

Community
  • 1
  • 1
G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40