class CholesterolPagingFragment: Fragment() {
companion object {
fun newInstance(): CholesterolPagingFragment {
val args = Bundle()
val fragment = CholesterolPagingFragment()
fragment.arguments = args
return fragment
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_paging_cholesterol, container, false)
return view
}
}
I have wrote the above code in Kotlin to initialize a fragment. Though I can't figure out a way to pass a list of objects(eg: List<Human>
) to this fragment. I have tried with the Bundle()
but couldn't find a proper way.
You should create a method within your fragment that accepts the type of object you wish to pass into it. In this case i named it "setObject" (creative huh? :) ) That method can then perform whatever action you need with that object. Show activity on this post. Passing arguments by bundle is restricted to some data types.
In fragment : Create a reference variable for the class whose object you want in the fragment. Simply create a setter method for the reference variable and call the setter before replacing fragment from the activity. 2. In activity: Show activity on this post. First store the objects in Bundle, don't forget to put implements serializable in class.
Simply create a setter method for the reference variable and call the setter before replacing fragment from the activity. 2. In activity: Show activity on this post. First store the objects in Bundle, don't forget to put implements serializable in class.
In this technique, we have used the Serialization for the list passing. Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.
You can pass List<Human>
as Parcelable
ArrayList
to the Bundle as follows:
companion object {
fun newInstance(myList : ArrayList<Human>): MyFragment {
val args = Bundle()
args.putParcelableArrayList("list",myList);
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
}
To retrieve data:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val args = arguments
var myList : ArrayList<Human> = args.getParcelableArrayList<Human>("list")
}
Do Parcelable
implementation in Human
Data Class :
data class Human(val name: String, val phone: String) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString()) {
}
override fun writeToParcel(dest: Parcel?, flags: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun describeContents(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
companion object CREATOR : Parcelable.Creator<Human> {
override fun createFromParcel(parcel: Parcel): Human {
return Human(parcel)
}
override fun newArray(size: Int): Array<Human?> {
return arrayOfNulls(size)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With