0

I'm beginner and I don't understand how objects in Android work.

For example, I'm trying to do simple game. Game has two teams and each team has two players. Every team has each turn of a game, and in each turn plays only one player, so I have to remember when it is each players turn and score of teams.

I'm thinking of creating the class Game, with variables NextTeam (where I store information about team which is playing next), NextFirstTeamPlayer, NextSecondTeamPlayer, FirstTeamScore and SecondTeamScore.

Also I have two activities which I need during the game. I start them and destroy them.

My questions are:

1) If I create object of Game class in one activity, will that object be destroyed when I destroy activity in which object was created?

2) What is the best way to pass that object from one activity to another? My only idea is to pass values of object variables to new activity which is started and in new activity create new object with passed values. Is there any better way to do it?

3) I would like to use Game class in few activities. I have declared class as separated Java file. How to include that file in every activity in which I want to use that class?

Alejandro
  • 7,290
  • 4
  • 34
  • 59
user3100193
  • 531
  • 4
  • 6
  • 16
  • if data is small then you can go with shared pref else you can possibly use sqlite in android – KOTIOS Feb 07 '15 at 17:55
  • You need to Extend Application class to create a Global variable that is available to all the classes. – Muthu Feb 07 '15 at 17:58
  • data is small, 4 strings and 2 integers. Wouldn't it take long to store data in sql every time and return it every time when it is needed? – user3100193 Feb 07 '15 at 18:04

2 Answers2

2

1) if I create object of Game class in one activity, will that object be destroyed when I destroy activity in which object was created?

Yes, at some point it will be destroyed from the memory.

2) what is the best way to pass that object from one activity to another? My only idea is to pass values of object variables to new activity which is started and in new activity create new object with passed values. Is there any better way to do it?

You can pass an object using Intents. Object should either implement Serializable class or Parcelable. The second is faster. More info here.

3) I would like to use Game class in few activities. I have declared class as separated java file. How to include that file in every activity in which I want to use that class?

Import it using the import keyword at the top of each class you need to use it.

Have you thought of what is called the Singleton pattern for the Game object? I suppose that you only have one Game instance each time, so it's probably a good idea.

Community
  • 1
  • 1
Thanos
  • 3,627
  • 3
  • 24
  • 35
1

You need to Extend Application class to create a Global variable that is available to all the classes. you Should be doing something similar like this.

public class Globals extends Application{
   private int data=200;

   public int getData(){
     return this.data;
   }

   public void setData(int d){
     this.data=d;
   }
}

Add this class name to manifest

<application
   android:name=".Globals"
   .... />

and you will be calling it like

Globals g = (Globals)getApplication();
int data=g.getData();
Muthu
  • 1,022
  • 1
  • 7
  • 17