You can use parcel.writeString(student.getClas().getName());
and student.setClas(Class.forName(in.readString());
Android:How to use Class<? extends Activity> as a variable type in Parcelable
-
02-09-2022 - |
Domanda
I'm having problem about writing and reading a variable whose type is Class<? extends Activity>
but I could find any solution for this.
clas is variable name whose type is Class<? extends Activity>
. I'm using this variable for opening related activity, when an student clicked in a listactivity
There is no function like Parcelable.readClass()
or writeClass()
can anyone help please to solve this
Thx in advance
Here is my Student.java
public class Student {
private String id;
private Class<? extends Activity> clas;
...
}
Here I created ParcalebleStudent.java
public class ParcalebleStudent implements Parcelable {
private Student student;
....
private ParcalebleStudent(Parcel in) {
student = new Student();
student.setId(in.readString());
student.setClas(in.readWhat?????????);//I need help here
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(student.getId());
parcel.writeWhat???????(student.getClas());//and here
}
}
Soluzione
Altri suggerimenti
Class
is Serializable, so you could use writeSerializable
and readSerializable
. You'll then have to cast it back to Class<? extends Activity>
. Most IDEs will give you an unsafe cast warning, but there's not a great way to do it in a manner where you avoid that warning.