1

What I am interested in doing is creating a separate class, similar to a Model in a Model-View-Controller relationship, in which I can store all of my data so that each Activity can access its functions. I have started reading about Intents, but it still doesn't seem to be exactly what I'm looking for.

Am I not understanding Intents correctly? Or is there another way to do this?

yasar
  • 13,158
  • 28
  • 95
  • 160
JuiCe
  • 4,132
  • 16
  • 68
  • 119
  • Better try the [Getter & Setter](http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html) – Praveenkumar Jun 06 '12 at 14:43
  • make a java class as opposed to an activity – owen gerig Jun 06 '12 at 14:43
  • I understand Getter & Setter methods, but that wouldn't be the problem. I can write the class to store the information just fine, I'm just unsure how to initialize the object somewhere where each Activity can use it, unless I just pass it to each Activity as a parameter? Owen - – JuiCe Jun 06 '12 at 14:46
  • Sorry, Owen, I'm new to Android, but from everything I have found out an Activity needs to start the Android application. I understand the data can be stored in just a regular class. – JuiCe Jun 06 '12 at 14:47
  • Have you look at: http://stackoverflow.com/questions/4878159/android-whats-the-best-way-to-share-data-between-activities or this one http://www.jameselsey.co.uk/blogs/techblog/android-implementing-global-state-share-data-between-activities-and-across-your-application/ – Nolesh Jun 06 '12 at 14:52
  • make `Parcelable` Model class by implementing `Parcelable`. and pass it from one activity to another activity using Intent. initialize it in your first activity and to keep its existence pass it further in your next activity to be used. – Ravi1187342 Jun 06 '12 at 15:45
  • you can also use SharedPreferences to store data in it and you can access data from this SharedPreferences in every activity. – Ravi1187342 Jun 06 '12 at 15:52

3 Answers3

1

There is a good answer on your question: http://chrisrisner.com/31-Days-of-Android--Day-7%E2%80%93Sharing-Data-Between-Activities

Nolesh
  • 6,848
  • 12
  • 75
  • 112
0

If you are using Intents then use can pass the object in putExtra. This SO post has a good example

Moreover use a singleton pattern so each class has reference to same object.

Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24
0

You can also subclass the Application class and access that singleton instance from all Activities & Services

Philippe Girolami
  • 1,876
  • 1
  • 13
  • 15