2

I am building an app with a pretty decent UI, and thus has multiple activities and classes. Most of the classes are relative to a single activity or single other class, how ever: I need to create a "class" that can store/pass information and perform functions from multiple other activites and classes.

I am worried that when I create a new object of the "class" in each other activity/class that it will erase or call a new instance for this "class", where as I need the data to stay put while I go from activity to activity, to be able to be called upon and used/changed, and be acessible throughout the application.

Is there any special way to do this?

Thank you.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • 5
    Check out the Singleton design pattern. – Kylar Dec 28 '11 at 03:25
  • [take a look at this thread][1] [1]: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – since2006 Dec 28 '11 at 03:27
  • 1
    A Singleton is the way to go. Just be careful to not keep any references to the activities themselves in the singleton. That's a great way to create a memory leak that can bring your app crashing down. – Ted Hopp Dec 28 '11 at 03:28
  • interesting, but I am trying to do much more then save the state. the singleton might need more looking into. – WIllJBD Dec 28 '11 at 03:30
  • Isn't this what the Application class is meant for...? I'm not sure I follow the question correctly. (Never mind, I now see @since2006 had the same thought...) – BRPocock Dec 28 '11 at 03:50

2 Answers2

1

you can use sharedpreferences .If you dont have many variable else create a class with all

public static "variables";
Its not blank
  • 3,055
  • 22
  • 37
0

Do not implement the singleton design pattern; this is the reason for listeners. A UI component registers a bunch of listeners (i.e. model and/or controller objects). When an event occurs, listeners are notified and update their state accordingly. There is no reason to delegate the handling of messages to a proxy object, such as the singleton in this case.

Singleton pattern: Drawbacks

mre
  • 43,520
  • 33
  • 120
  • 170