0

I've got a simple listView which calls another fragment to update the selected driver:

public static ArrayList<Driver> allDrivers = new ArrayList<>(); //statict just so i can checj if the value is automatically changed in the other fragment, which is unfourtantly the case
DriverListAdapter adapter;
ListView listViewDriver;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   
    final DriverUpdateFragment fragment = new DriverUpdateFragment();
    Driver selectedDriver2 = (Driver) parent.getItemAtPosition(position);
    Fragment ft = getFragmentManager().findFragmentByTag("DriverListFragment");
    fragment.setTargetFragment(ft, ResultCodeConstants.DRIVER_UPDATE_SUCCESS);
    Bundle bundle = new Bundle();
    // bundle.putSerializable("mySelectedDriver", selectedDriver2);
    bundle.putParcelable("mySelectedDriver", selectedDriver2);       
    fragment.setArguments(bundle);
    final FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.frameLHaupt, fragment, "DriverUpdateFragment");
    transaction.addToBackStack(null);
    transaction.commit();
}

In the DriverUpdateFragment I just assign a random driverName in order just to check if the value will be automatically changed in the allDrivers ArrayList:

//Driver selectedDrivertest = (Driver) getArguments().getSerializable("mySelectedDriver");
Driver selectedDrivertest = (Driver) getArguments().getParcelable("mySelectedDriver");
selectedDrivertest.setDriverName("hey i am hardcoded updated");
Log.e("why is the value changed already in the main fragment???",""+ DriverListFragment.allDrivers); //here the value changes already. shouldn't the object be passed by value??????

My POJO class looks like this:

@Entity(tableName = "Driver",indices = {@Index(value = {"driverName"},unique = true)})
public class Driver implements Serializable, Parcelable {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    @NotNull
    private int id;

    @ColumnInfo(name = "firstName")
    @NotNull
    private String firstName;

    @ColumnInfo(name = "lastName")
    @NotNull
    private String lastName;

    @ColumnInfo(name = "driverName")
    @NotNull
    private String driverName;

    public Driver(@NotNull String firstName, @NotNull String lastName, @NotNull String driverName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.driverName = driverName;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotNull
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(@NotNull String firstName) {
        this.firstName = firstName;
    }

    @NotNull
    public String getLastName() {
        return lastName;
    }

    public void setLastName(@NotNull String lastName) {
        this.lastName = lastName;
    }

    @NotNull
    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(@NotNull String driverName) {
        this.driverName = driverName;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Driver driver = (Driver) o;
        return getId() == driver.getId();
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.firstName);
        dest.writeString(this.lastName);
        dest.writeString(this.driverName);
    }

    public void readFromParcel(Parcel source) {
        this.id = source.readInt();
        this.firstName = source.readString();
        this.lastName = source.readString();
        this.driverName = source.readString();
    }

    protected Driver(Parcel in) {
        this.id = in.readInt();
        this.firstName = in.readString();
        this.lastName = in.readString();
        this.driverName = in.readString();

    }

    public static final Parcelable.Creator<Driver> CREATOR = new Parcelable.Creator<Driver>() {
        @Override
        public Driver createFromParcel(Parcel source) {
            return new Driver(source);
        }

        @Override
        public Driver[] newArray(int size) {
            return new Driver[size];
        }
    };
}

The issue is that the arraylist already updates even tough Java objects are always passed by value according to the documentation?

what am I missing?

Since when are values passed by reference in Java?

I tried using parceable and serializable.

Nummer Eins
  • 199
  • 1
  • 8
  • Might check this 'https://stackoverflow.com/questions/34770149/passing-objects-from-activity-to-fragments-is-pass-by-reference' out :) – Lebron11 May 28 '21 at 18:39
  • 1
    Well, objects are always passed by reference in Java (in the sense that a reference to the object is passed by value). Only primitives are passed by value in Java. For example, if you pass an `ArrayList` in Java, the called method does not receive a copy of the `ArrayList`, it gets a reference to the `ArrayList` and can modify the `ArrayList` any way it wants to. – David Wasser May 29 '21 at 20:18

0 Answers0