0

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

Alon
  • 2,919
  • 6
  • 27
  • 47
  • When you put your object in a bundle, it is serialized -- which means, it's turned into a stream representation of your object. So, yes, a copy is made. If you need to make changes, you either need to pass a copy back using `onActivityResult()`, or store a reference to the object outside of the activity. Or use a [Static Singleton](http://stackoverflow.com/questions/15368815/general-android-advice-global-variables/15368906#15368906) – 323go Sep 08 '14 at 17:16

3 Answers3

2

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 (e.g. SomeActivity.java):

Student appStudent = ((MyApp) getActivity().getApplicationContext()).getMyObject();
appStudent.Name = "New Name"; // "global" update
alpinescrambler
  • 1,934
  • 16
  • 26
  • how I configure the object as singleton?? i know that I can to it for a class but not for object. If I do it for the class it cause that I cant make another instance of this class. another way is to make the object static but i dont know if its good. – Alon Sep 08 '14 at 17:15
  • 1
    Declare a class that extends Application (e.g. public class MyApp extends Application ). Declare your object there, and implement a getter (if you want). Then anywhere in your code(s), just do myOject = ((MyApp) getActivity().getApplicationContext()).getMyObject(); – alpinescrambler Sep 08 '14 at 17:19
  • 1
    Oh and make sure you update your manifest as well to point to this MyApp thingy that you declared ( – alpinescrambler Sep 08 '14 at 17:20
  • same way I can do its in the activity cause I dont kill the first activity until the app is close. – Alon Sep 08 '14 at 17:22
  • 1
    I'm not sure what you mean, yes you didn't kill the "first activity" when you switched to another one in you app, but behind the scenes, the android OS may kill it if it needs to. – alpinescrambler Sep 08 '14 at 17:29
  • @alpinescrambler : The approach used in this answer really isn't suitable for a scenario where there may be more than one `Student` object. Further to that, extending the `Application` class is something which should only be done when necessary and not for a general purpose of sharing objects. – Squonk Sep 08 '14 at 18:13
0

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.

bbrakenhoff
  • 179
  • 1
  • 6
0

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);
}
}
Community
  • 1
  • 1
danny117
  • 5,581
  • 1
  • 26
  • 35