6

I am going to make application more specifically a game for android phone.

I am going to have multiple activities and many classes so I'm thinking about storage of the game data.

Game data should be visible for activities so I'm thinking if I should rather use singleton to store data there and receive it easily from any activity or should I rather pass data using Intents?

There will be small amounts and large amounts of data (small such as score, large such as maps etc..).

For scores I would use intents but then wouldn't it be better to do everything the same way? And if yes I think one singleton with whole game state would be better. Any ideas?

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Taks
  • 2,033
  • 4
  • 18
  • 23
  • 1
    If you need the same data visible to multiple Activities, use a singleton. If you just need to notify the next Activity of some particular data, use extras in your Intent. Personally, I think it's better to use both; they're not mutually exclusive. – Glendon Trullinger Jul 10 '11 at 21:24
  • 1
    Since it's a game another good fit would be ServiceLocator: http://gameprogrammingpatterns.com/service-locator.html – mibollma Jul 10 '11 at 21:52

1 Answers1

1

When I need data that is used by multiple activities I have just created a custom Application class and then used that as my "Singleton" and it works fine since every activity can then access the custom Application context

To do that start by creating and extended Application class

public class MyApplication extends Application {
  // Details left blank
}

Add this to your manifest so it knows to use that instead of the default application

 <application 
        ...
        android:name=".MyApplication"

Then add any custom methods that you want all of your activities to have access to, and from each activity use something like

((MyApplication)this.getApplicationContext()).myMethod()

You can also see Application for more details

Idistic
  • 6,281
  • 2
  • 28
  • 38