简体   繁体   中英

Android - One class used by multiple activities

I have a class that manages the data that was loaded from a file. This class is initialized in the main Activity. When the main Activity creates a new Activity the new Activity needs the data from the file, in other words, it needs a reference to the class that manages the data. What's the best way to do this?

Yes, the best way is to create only one instance of your class. It's the Singleton design pattern.

The singleton pattern should suit your needs. This basically is a class that can only be instantiated once and manages that instance itself, so you can get it from anywhere.

A tutorial like this will help you get started: http://portabledroid.wordpress.com/2012/05/04/singletons-in-android/

If a class simply represents a chunk of data that it reads from a file, there's nothing wrong with making your class a singleton, like this:

class FileData {
    private static final FileData instance = readFile();
    public static FileData getInstance() {
         return instance;
    }
    private static readFile() {
        ... // Read the file, and create FileData from it
    }
    public int getImportantNumber() {
        return ...
    }
}

Now you can reference the data from all your other classes, like this:

FileData.getInstance().getImportantNumber();

1.: Singleton pattern
2.: You could make the class Parcelable.

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
private int mData;

/* everything below here is for implementing Parcelable */

// 99.9% of the time you can just ignore this
public int describeContents() {
    return 0;
}

// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(mData);
}

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
    public MyParcelable createFromParcel(Parcel in) {
        return new MyParcelable(in);
    }

    public MyParcelable[] newArray(int size) {
        return new MyParcelable[size];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private MyParcelable(Parcel in) {
    mData = in.readInt();
}

}

Then you are able to send your object through the intent:

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

And get it in your 2nd Activity like that:

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

For convenience I took the code from this SO thread because it's pretty good, but it is also pretty basic though. You could even send Lists of objects trough Intents with it, but that is a little bit more complicated and would need way more example code and explaination. If thats your objective, please ask. For one object the code is totally fine though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM