【问题标题】:Android - One class used by multiple activitiesAndroid - 多个活动使用的一个类
【发布时间】:2014-02-22 14:16:57
【问题描述】:

我有一个类来管理从文件加载的数据。这个类在主Activity中初始化。当主 Activity 创建一个新 Activity 时,新 Activity 需要文件中的数据,换句话说,它需要对管理数据的类的引用。最好的方法是什么?

【问题讨论】:

    标签: java android class android-activity


    【解决方案1】:

    是的,最好的方法是只为您的班级创建一个instance。这是单例设计模式。

    【讨论】:

      【解决方案2】:

      如果一个类只是表示它从文件中读取的一块数据,那么让你的类成为单例并没有错,如下所示:

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

      现在您可以引用所有其他类的数据,如下所示:

      FileData.getInstance().getImportantNumber();
      

      【讨论】:

      • 我将if(_instance == null) _instance = new FileData(); 添加到getInstance(),所以它只会实例化一次。
      【解决方案3】:

      singleton 模式应该适合您的需要。这基本上是一个只能实例化一次并自行管理该实例的类,因此您可以从任何地方获取它。

      这样的教程将帮助您入门:http://portabledroid.wordpress.com/2012/05/04/singletons-in-android/

      【讨论】:

        【解决方案4】:

        1.: 单例模式
        2.:你可以使类 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();
        }
        

        }

        然后你就可以通过意图发送你的对象了:

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

        然后像这样在你的第二个活动中得到它:

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

        为方便起见,我从this SO 线程中获取了代码,因为它非常好,但它也非常基本。您甚至可以使用它通过 Intents 发送对象列表,但这有点复杂,需要更多示例代码和解释。如果那是您的目标,请询问。不过对于一个对象,代码完全没问题。

        【讨论】:

          猜你喜欢
          • 2011-09-07
          • 2015-05-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-18
          • 1970-01-01
          • 2013-09-19
          • 1970-01-01
          相关资源
          最近更新 更多