首页 > 解决方案 > Is it possible to store data from two different parcelable data classes in one single intent to start a new Activitiy with it?

问题描述

after researching a lot, I wasn't able to find the real solution for my problem. The most answers are based on attaching single parcelable object to an intent and starting a new activity from it.

In my case, I use data from two different parcelable classes. I have two different RecyclerView Adapter classes that live in the same activity.

So, when the user clicks on a data set from the first (parent) Adapter class, the View shows all available (child) data sets from the second Adapter class.

I want to save the parent object data set where the user clicked first, as well the child object where the user performed a long click at last. The both objects need to be attached on a same intent and when done, a new activity should be started. The data from the both objects (parent and child) has to be passed in the new acticity.

For the sake of clearness, I'm posting only the relevant parts of the code.

This is the part of the parent adapter class where I need to save the object data to a sort of "global" intent, my code would produce only a local intent instead (that's why it's commented only).

                itemView.setOnClickListener {
                notifyItemChanged(selectedPos)
                selectedPos = adapterPosition
                notifyItemChanged(selectedPos)
                listener.onHeadItemClicked(weKopf.WENR, p1)
                // save parent parcelable object data to "global" intent 
                // val intent = Intent(context, StorageGoodsActivity::class.java)
                // intent.putExtra("weKopf", weKopf)

            }

Here is the part of the code where I want to attach the child object data to the same intent and then start the new activity.

                itemView.setOnLongClickListener{
                notifyItemChanged(selectedPos)
                selectedPos = adapterPosition
                notifyItemChanged(selectedPos)
                listener.onPositionLongClicked(wePos.lfdNr.toInt())
                /**
                 * setting-up for passing objects between activities
                 */
                val intent = Intent(context, StorageGoodsActivity::class.java)
                //intent.putExtra("weKopf", weKopf)
                intent.putExtra("wePos", wePos)
                context.startActivity(intent)
                true

What is the most elegant way to do this? My idea was to store the intent into a shared component, but this intent should live as long as the user clicks the back button to return to the old activity and chooses new parent-child constellation after that. If so, will this created item be thrown away, or it still lives in the shared component?

I'm quite new with Android Studio and Kotlin and I appreciate every help or advice. Thank you so much in advance.

标签: androidandroid-intentkotlinandroid-activityparcelable

解决方案


You can have as many extras as you want in an Intent, so long as each gets its own name. The only limitation is a limitation on the total size of an Intent (about 2 MB iirc).


推荐阅读