Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clone an object in android?

Tags:

java

android

What would be the best way to copy/clone an object in java/android?

rlBodyDataObj rlbo = bdoTable.get(name);

Right now the code assigns an object from a hashTable, yet I need to get a clone of it, so that I'd be able to use it multiple times.

like image 814
Roger Travis Avatar asked May 17 '12 12:05

Roger Travis


People also ask

How do you copy objects on Android?

You have to specify on your class that it implements Cloneable interface and you have to override clone method inside that class. By Default it will use clone method of Object class.

How do you use the clone method of an object?

To use the Object. clone() method, we have to change a lot of syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object. clone() etc. We have to implement cloneable interface while it doesn't have any methods in it.

How do you clone an object in Java?

Java Object Cloning If you want to use Java Object clone() method, you have to implement the java. lang. Cloneable marker interface. Otherwise, it will throw CloneNotSupportedException at runtime.


4 Answers

Make sure that your DataObj class implements Cloneable and add the following method

protected Object clone() throws CloneNotSupportedException {
        return super.clone();
}

Then you should be able to call (DataObj)rlBodyDataObj.clone(); to get a clean copy (note the cast).

like image 114
Damian Avatar answered Oct 06 '22 09:10

Damian


class Test implements Cloneable
  {
   ...
      public Object clone()
      {
          try
      {
              return super.clone();
          }
      catch( CloneNotSupportedException e )
      {
              return null;
          }
      } 
  ...
  }
like image 34
Lola Avatar answered Oct 06 '22 07:10

Lola


you can implements Parcelable (easy with studio plugin), and then

public static <T extends Parcelable> T copy(T orig) {
    Parcel p = Parcel.obtain();
    orig.writeToParcel(p, 0);
    p.setDataPosition(0);
    T copy = null;
    try {
        copy = (T) orig.getClass().getDeclaredConstructor(new Class[]{Parcel.class}).newInstance(p);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return copy;
}
like image 6
user2474486 Avatar answered Oct 06 '22 09:10

user2474486


Sometimes you need to modify some fields before returning from the clone() method.

Check this : http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#clone(). I pasted the relevant part here for convenience:

"By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified."

like image 3
Hakan Serce Avatar answered Oct 06 '22 07:10

Hakan Serce