代码之家  ›  专栏  ›  技术社区  ›  Alon

将对象传递给另一个活动

  •  0
  • Alon  · 技术社区  · 12 年前

    我找到了这样做的方法

    Student student = new Student (18,"Z r");
    Intent i = new Intent(this, B.class);
    i.putExtra("studentObject", student);
    startActivity(i);
    

    问题是,如果对象在第一个活动中发生了更改,则在另一个活动中没有发生更改。 我想如何像构造函数一样,传递对象的副本,而传递对象本身。

    谢谢

    3 回复  |  直到 10 年前
        1
  •  2
  •   alpinescrambler    12 年前

    如果您将“对象”配置为整个应用程序的单例,情况如何?这样,每个人(你的应用程序)都会看到变化。。。请在此处查看一些见解: http://developer.android.com/guide/faq/framework.html#3

    例如,在其他文件(Student.java)中:

    public class Student {
      public String Name;
    }
    

    创建自定义应用程序类:

    public MyApp extends Application {
       private Student obj = new Student();
    
       public Student getMyObject() {
           return obj;
       }       
    
    }
    

    应用程序中的任何位置(例如SomeActivity.java):

    Student appStudent = ((MyApp) getActivity().getApplicationContext()).getMyObject();
    appStudent.Name = "New Name"; // "global" update
    
        2
  •  0
  •   bbrakenhoff    12 年前

    您还可以查看BroadcastReceiver。使用BroadcastReceiver,您可以将消息从一个活动发送到另一个活动,并且使用接口可以将对象从一个“活动”传递到另一活动。

    我想 this 是一个很好的例子,其中创建BroadcastReceiver来检查设备的互联网连接。但是您可以使用自己的自定义操作在BroadcastReceiver中轻松地转换它以发送对象。

        3
  •  0
  •   Community Mohan Dere    9 年前

    在你的学生课堂上实施parcelable,你可以将学生复制到意图中。

    How can I make my custom objects Parcelable?

    代码与可分割类一起工作

    > Student student = new Student (18,"Zar E Ahmer"); Intent i = new
    > Intent(this, B.class); i.putExtra("studentObject", student);
    > startActivity(i);
    

    下面是我使用的实现parcelable的bean类的示例。在这里,您可以用Student替换KmlMarkerOptions

    @SuppressLint("ParcelCreator")
    public class KmlMarkerOptions implements Parcelable {
    
    public MarkerOptions markeroptions = new MarkerOptions();
    public String href = "";
    public int hrefhash =-1;
    public String id = "";
    public long imageId = -1;
    public int locationId = -1;
    public int markerSize = -1;
    
    public KmlMarkerOptions(){
    
    }
    
    public KmlMarkerOptions(Parcel in) {
        this.markeroptions = in.readParcelable(null);
        this.href = in.readString();
        this.hrefhash = in.readInt();
        this.id = in.readString();
        this.imageId = in.readLong();
        this.locationId = in.readInt();
        this.markerSize = in.readInt();
    }
    
    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public KmlSummary createFromParcel(Parcel in) {
            return new KmlSummary(in);
        }
    
        public KmlSummary[] newArray(int size) {
            return new KmlSummary[size];
        }
    };
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(markeroptions, 0);
        dest.writeString(href);
        dest.writeInt(hrefhash);
        dest.writeString(id);
        dest.writeLong(imageId);
        dest.writeInt(locationId);
        dest.writeInt(markerSize);
    }
    }