开发者

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 accessi开发者_C百科ble (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.


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


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


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


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


how about use a static keyword ?

public static SomeClass someObject

in your activity class that initiate your object


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜