1

In my application I am trying to pass a generic worker object (which is parcelable) between activities. However, when I get parcelableExtra from intent, it creates new instance of the worker.

As you can see from the code, I assigned test variable to 10 then put it to bundle, when get it from new activity, it gives 15 as default value.

Is there a way to pass same instance reference between activities?

Put worker to intent:

fun newIntent(context: Context, worker: PlayerVideoContentsWorker, position: Int): Intent {
            val intent = Intent(context, PlayerVideoContentsActivity::class.java)
            worker.test = 10
            intent.putExtra(WORKER, worker)
            intent.putExtra(POSITION, position)
            return intent
        }

get worker from intent

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val worker = intent.getParcelableExtra<PlayerVideoContentsWorker>(WORKER)
    print(worker?.test) //prints 15
    ...

PlayerVideoContentsWorker.kt

@Parcelize
class PlayerVideoContentsWorker(val service: PlayerPaginatableListService<VideoContent, CursorPagination>,
                                val paginationInfo: CursorPaginationInfo)
    : PlayerPaginatableListWorker<VideoContent, CursorPagination>(service, paginationInfo), Parcelable

PlayerPaginatableListWorker

open class PlayerPaginatableListWorker<T : Parcelable, P : IPaginationData>(
        private val service: PlayerPaginatableListService<T, P>,
        private val paginationInfo: CommonPaginationInfo<P>) {
    var test = 15
    ...
alian
  • 204
  • 1
  • 12
  • Does this answer your question? [Passing complex object between activity](https://stackoverflow.com/questions/21931117/passing-complex-object-between-activity) – Ivo Apr 06 '22 at 12:40

2 Answers2

1

You can't make a class Parcelable by just putting @Parcelize above it. @Parcelize is a conventient plugin for making a class Parcelable but the class needs to have certain requirements.

As stated in the documentation:

@Parcelize requires all serialized properties to be declared in the primary constructor

This is not the case for test

Furthermore, the parameters are not of the types listed at the Supported types section there.

Ivo
  • 18,659
  • 2
  • 23
  • 35
  • Thanks for the clarification. Besides test variable, service and paginationInfo come null from intent and these classes are also implement parcelable, which means they are supported type. What could be the problem? – alian Apr 06 '22 at 12:53
  • @alian ah ok, I thought they weren't. In that case I don't know why they turn up to be null – Ivo Apr 06 '22 at 12:58
0

However, when I get parcelableExtra from intent, it creates new instance of the worker.

Correct. That is how Intent extras work. Parcelable is a serialization mechanism — think of it as being akin to passing JSON around.

Is there a way to pass same instance reference between activities?

No. Have just one activity, with separate screens implemented via fragments or composables.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • well I understand passing same instance is not possible, it is hard to change single activity architecture. Then is there way to get the worker class with the assigned data in new activity? – alian Apr 06 '22 at 12:27
  • @alian: "Then is there way to get the worker class with the assigned data in new activity? " -- I cannot answer that, as I do not know what most of that stuff is. – CommonsWare Apr 06 '22 at 12:37