2

Let's assume I have a class MainActivity.

This contains a number of objects stored in fields, such as instances of Player, Enemy, Level, etc. Each of these objects needs to be able to refer to every other object.

What is the best way to go about this?

  1. Make these fields static, and refer to them accordingly, i.e.

    MainActivity.player.setHealth(0);
    
  2. Create getter methods for each field, and simply pass each object a reference to MainActivity, so that they can call these getter methods, i.e.

    mainActivity.getPlayer().setHealth(0);
    
  3. Pass each object a reference to every other object, and store these references in fields within each object, so that they can be referred to directly, i.e.

     player.setHealth(0);
    
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Dan
  • 1,198
  • 4
  • 17
  • 34
  • In your simple scenario... if the Player is only 1, then it makes sense for you to have a seperate class for it, and have a static on that Player.setHealth() getHealth() instead of going to the main activity. -- Player should be a singleton. So should Level, as these things you can only have one at a time. – sprocket12 Jan 03 '14 at 11:04
  • So if there is only 1 Player, make it entirely static? What if there are 2 or more Players? – Dan Jan 06 '14 at 13:11
  • If you only have 1 player active at a time then I would use a singleton player class, otherwise not. – sprocket12 Jan 06 '14 at 14:03

3 Answers3

3

Not a real answer but just to give you some tips.

Your Player should be like so:

public class Player
{
    private static Player _player = null;
    int _health;
    ...

    public static Player getInstance()
    {
        if (_player == null)
            _player = new Player(...);

        return _player;
    }

    public void increaseHealth(int amount)
    {
        _health += amount;
    }
 }

Then in any part of your application when you need a Player you can do:

Player p = Player.getInstance();

and you will get the same player all the time. You can do a similar thing with your level class as only 1 level will be active at any one time.

However the Enemy class will need a different approach. I would make a List inside the Level class and get at them like so:

Level l = Level.getInstance();
List<Enemy> enemiesOnLevel = l.getEnemies();
// do something with them
sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • In the end I did use the Singleton pattern, not just for the Player but for a "GameState" class that holds all the data pertaining to the current state of the game. – Dan Jan 27 '14 at 09:51
0

Have a look in the Android docs here: http://developer.android.com/guide/faq/framework.html#3. There is also the possibility to serialize your object into primitive datatypes and pass those within your Intent to the new Activity.

funglejunk
  • 288
  • 2
  • 9
  • I am talking about objects being able to reference each other within a single Activity, not between multiple Activities. – Dan Jan 06 '14 at 13:10
0

A couple more options to share objects between activities are to use parcable, which I think is probably the highest performance method, and shared preferences.

In my app I used to learn (the little I know about android programming), I used gson to serialize the object to json, then stored it in shared preferences in activity A , then recreated it from shared preferences in activity B, and then stored it again.

nPn
  • 16,254
  • 9
  • 35
  • 58