简体   繁体   中英

pass Object to another activity

I have found this way to do this

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

The problem is that if the object changed in the first activity No change took place in the another activity. I thought how to make it like a constructor that no copy of the object is pass but the object it self.

Thanks

How about if you configure the "object" as a singleton of the entire application? This way, everybody (your app) sees the changes... See some insights here: http://developer.android.com/guide/faq/framework.html#3

For example, in some other file (Student.java):

public class Student {
  public String Name;
}

Create a custom application class:

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

   public Student getMyObject() {
       return obj;
   }       

}

Anywhere in your application (eg SomeActivity.java):

Student appStudent = ((MyApp) getActivity().getApplicationContext()).getMyObject();
appStudent.Name = "New Name"; // "global" update

You could also look into a BroadcastReceiver. With a BroadcastReceiver you can send a message from one Activity to another and with an interface you can pass the object from one Activity to the other.

I think this is a great example, where a BroadcastReceiver is created to check the internet connection of the device. But you can easily convert this in a BroadcastReceiver with your own custom action to send the object.

Implement parcelable in your student class and you can copy the student into the intent.

How can I make my custom objects Parcelable?

Code works with parcelable classes

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

Below is an example of bean class I use that implements parcelable. Here you would replace KmlMarkerOptions with Student

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

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