Singleton vs. Intent (Android)

I am going to make the application more specifically a game for an Android phone.

I will have several actions and many classes, so I'm thinking about storing game data.

Game data should be visible for action, so I think that if I prefer to use singleton to store data there and get it easily from any activity, or is it better to transfer data using Intents?

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

For evaluations, I would use intentions, but then it would be better to do everything the same way? And if so, I think one singleton with a whole game state would be better. Any ideas?

+6
source share
1 answer

When I need data that is used by several actions, I just created my own application class and then used it as my "Singleton", and it works fine, since each action can then access the application’s custom context

To do this, create and extend the application class.

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

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

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

Then add any custom methods you want all your actions to have access to, and from each action use something like

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

You can also see Application for more details.

+1
source

Source: https://habr.com/ru/post/892448/


All Articles