A. Steenbergen
A. Steenbergen

Reputation: 3440

Creating an Object accessible by all Activities in Android

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.

Upvotes: 9

Views: 10628

Answers (7)

AAYUSH CUBING
AAYUSH CUBING

Reputation: 33

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;
    }
}

Upvotes: 0

Lalit Poptani
Lalit Poptani

Reputation: 67296

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();
    ...
  }
}

Upvotes: 3

Yann Masoch
Yann Masoch

Reputation: 1630

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

Upvotes: 0

Nic
Nic

Reputation: 2864

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

Upvotes: 16

blessanm86
blessanm86

Reputation: 31789

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();

Upvotes: 7

user555742
user555742

Reputation: 68

how about use a static keyword ?

public static SomeClass someObject

in your activity class that initiate your object

Upvotes: 0

Vineet Shukla
Vineet Shukla

Reputation: 24031

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

Upvotes: 1

Related Questions