8

I'm trying to create an ArrayList of Data containing Objects (Like a list of Addresses and properties (pretty complex)) and am wondering: How can I make an Object accessible (and editable) by all Activities and not just the one it was instanciated in?

Basically this:

  1. Create Array in Activity 1
  2. Access same Array in Activity 2 and 3
  3. ???
  4. Profit.
Cœur
  • 37,241
  • 25
  • 195
  • 267
A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51

7 Answers7

16

The easiest way to do this is by creating an Singleton. It's a kind of object that only can be created once, and if you try to access it again it will return the existing instance of the object. Inside this you can hold your array.

public class Singleton  {
    private static final Singleton instance = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {
    }

    public static Singleton getInstance() {
        return instance;
    }

}

Read more about singleton: http://en.wikipedia.org/wiki/Singleton_pattern

Nic
  • 2,864
  • 1
  • 16
  • 21
  • Thanks for the answer! Would this also be the most ressource efficient way? How ressource efficient are singletons? – A. Steenbergen Sep 10 '11 at 16:13
  • 4
    The bad things about Singleton: Since there is no actual owner of the object garbagecollector will never remove it from memory, so it will take up that memory aslong as the app is active (until onDestroy). But then we also can consider that you don't have to create new objects all the time, which gives you better speed and less memory use. – Nic Sep 11 '11 at 13:26
7

You can extend the application class. And add your arrays there.

You can access the instance of the class by using this command

MyApplication appContext = (MyApplication)getApplicationContext();
Macarse
  • 91,829
  • 44
  • 175
  • 230
blessanm86
  • 31,439
  • 14
  • 68
  • 79
3

Well you can create a Constant class and declare you ArrayList as a static variable.

1.)

 Class ConstantCodes{

         public static ArrayList<MyClass> list = new ArrayList<MyClass>;
    }

This will be accessible from everywhere you want by just ConstantCodes.list

2.) You can extend your class by Application class like this

class Globalclass extends Application {

  private String myState;

  public String getState(){
    return myState;
  }
  public void setState(String s){
    myState = s;
  }
}

class TempActivity extends Activity {

  @Override
  public void onCreate(Bundle b){
    ...
    Globalclass appState = ((Globalclass)getApplicationContext());
    String state = appState.getState();
    ...
  }
}
Samir Alakbarov
  • 1,120
  • 11
  • 21
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

you should make it static and access it from any other activity.....

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0

You can create a java file x beside other java files. x file contains static method which used to access the class method without instantiate it. Now make a method called createVariable() and declare variable which you want to make it Global. Now make a method called getVariable() which returns the Global variable.

At which point you want to create global variable, call className.createVariable().

And to get access to that variable call className.getVariable(). Here is my example for Database class.

public class GlobalDatabaseHelper{

    static DatabaseHelper mydb;
    public static DatabaseHelper createDatabase(Context context)
    {
        mydb = new DatabaseHelper(context);
        return mydb;
    }

    public static DatabaseHelper returnDatabase()
    {
        return mydb;
    }
}
0

how about use a static keyword ?

public static SomeClass someObject

in your activity class that initiate your object

user555742
  • 68
  • 6
0

1- In your Activity1, déclare your array in public static

public static ArrayList<HashMap<String, String>> myArray = new ArrayList<HashMap<String, String>>();

2- In your Activity2, Activity3, etc. access to your ArrayList

Activity1.myArray
Yann Masoch
  • 1,628
  • 1
  • 15
  • 20