Send custom class object via Bundle between fragments with Parcebale

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Send custom class object via Bundle between fragments with Parcebale



I am using fragments inside my main activity and I want to send an object of my custom class "TaskWithUserAndProfile" to the TaskDetailsFragment


TaskWithUserAndProfile


TaskDetailsFragment



I found out that you can do it with Bundle and made it send a string, but things got complicated when I tried to send with Parcebale.
here are some parts of my code for better understanding:


Bundle


Parcebale



TaskWithUserAndProfile.kt


class TaskWithUserAndProfile() : Parcelable
override fun writeToParcel(p0: Parcel?, p1: Int)
TODO("not implemented") //To change body of created functions use File

var profile = Profile()
var task = Task()
var user = User()

constructor(parcel: Parcel) : this()
//profile = parcel.read


override fun describeContents(): Int
return 0


companion object CREATOR : Parcelable.Creator<TaskWithUserAndProfile>
override fun createFromParcel(parcel: Parcel): TaskWithUserAndProfile
return TaskWithUserAndProfile(parcel)


override fun newArray(size: Int): Array<TaskWithUserAndProfile?>
return arrayOfNulls(size)





HomeFragment.kt


//Inside onCreateView
adapter = TasksAdapter(tasksArray) item ->
println(item.profile)
val bundle = Bundle()
bundle.putParcelable("MyItem", item)
val taskDetailsFragment = TaskDetailsFragment()
taskDetailsFragment.arguments = bundle
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, taskDetailsFragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()



How should my class that implements the Parcebale look like and how can I then send and receive the item object in fragments?





this is not best method to send object from one activity to another .
– Muhammad Hassaan
Aug 1 at 11:15





use public method iniside fragment to update fragment data and use call backback listeners to send data from fragment back to activity and then to next fragment
– Muhammad Hassaan
Aug 1 at 11:16





It probably doesn't work because you didn't implement writeToParcel.
– EpicPandaForce
Aug 1 at 11:44


writeToParcel




3 Answers
3



You don't need to use Parcelable even, just simply define an TaskWithUserAndProfile variable in your TaskDetailsFragment and set in in HomeFragment.


Parcelable


TaskWithUserAndProfile


TaskDetailsFragment


HomeFragment



TaskDetailsFragment.kt


class TaskDetailsFragment : Fragment()

var selectedTask: TaskWithUserAndProfile? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View?




HomeFragment.kt


//Inside onCreateView
adapter = TasksAdapter(tasksArray) item ->
val taskDetailsFragment = TaskDetailsFragment()
taskDetailsFragment.selectedTask = item
val fragmentTransaction =
fragmentManager?.beginTransaction()

fragmentTransaction?.replace(R.id.container, taskDetailsFragment)

fragmentTransaction?.addToBackStack(null)
fragmentTransaction?.commit()



if you wanna keep using parcelize, just try this sample:


@Parcelize
data class TaskWithUserAndProfile(var profile:Profile, var task :Task, var user:User) : Parcelable



I could miss something from your class but the idea should looks like this, so use annotation @Parcelize and Parcelable implementation (do not need to override any method).



Update
Thanks for reminder. You will have to add this to your gradle file:


Update


androidExtensions
experimental = true





Make sure to enable experimental flag on kotlin-android-extensions. Good news though, it doesn't feel experimental, it works just fine
– EpicPandaForce
Aug 1 at 11:44





I'm quite new to Android, could you please elaburate more on how exactly I should do this? like where shall I put this code? how to send data how to recieve? how will my class look like?
– Mujtaba
Aug 1 at 12:21





I am not sure what do you want to achieve. Are you going to open new activity on click of your list item? And do you want to update it after some changes in this activity (when user went back to list of items activity)?
– Ikazuchi
Aug 1 at 13:10



Use this plugin:



android-parcelable-intellij-plugin-kotlin



for TaskWithUserAndProfile, Profile, Task, User models.


TaskWithUserAndProfile, Profile, Task, User





could you please explain a bit more? with some sample code based on my code
– Mujtaba
Aug 1 at 12:22





Open settings/plugins/browse repositories... then find Parcelable Code Generator(for kotlin). Install plugin and restart android studio. Then open your model and click Code/Generate/Parcelable(kotlin)
– UgAr0FF
Aug 1 at 12:34


settings/plugins/browse repositories...


Code/Generate/Parcelable(kotlin)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard