0

I've a problem with parceling an enum type via an intent, problem is with this line. intent.getParcelableArrayListExtra and the error is

not enough information to infer information for type variable T

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    super.onCreate(savedInstanceState, persistentState)
    setContentView(R.layout.main_activity)

    if (savedInstanceState == null)
        (fragment as DataFragment).setData(intent.getParcelableArrayListExtra(Application.BUNDLE_DATA) as ArrayList<DataEnum>)
}

Enum

@Parcelize
enum class DataEnum : Parcelable {

    Foo { override fun toString() = "Foo" },
    Bar { override fun toString() = "Bar" },
    Baz { override fun toString() = "Baz" };

    companion object {
        private fun list(): ArrayList<DataEnum> {
            return arrayListOf(
                FOO,
                BAR,
                BAZ
            )
        }
    }

}
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49
  • 1.) your `setData` will probably not work, you are supposed to be using `setArguments` before you add a fragment with a fragment transaction and 2.) why not just use `putSerializable` – EpicPandaForce Mar 05 '20 at 21:09
  • sorry didn't get you, I'm in an activity, setting data to a fragment, hope this helps – AppDeveloper Mar 05 '20 at 21:10
  • 1
    Exactly, a Fragment should be getting its data through `setArguments`. – EpicPandaForce Mar 05 '20 at 22:31
  • Thanks I understood you at `Activity to Fragment` communication advise via `setArguments` and I appreciate it, but problem here now is `activity to activity`, I could send the data from `ActivityA` via `putParcelableArrayListExtra` but I can't `getParcelableArrayListExtra` in `activityB`. Hope it's clear. – AppDeveloper Mar 06 '20 at 05:58

1 Answers1

0

https://developer.android.com/reference/java/lang/Enum

Enum extends Serializable

so use

Intent.putSerializable and Intent.getSerializable

and it'll all work out

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks Blundell, just for the sake of the argument can you please provide a parcelable example while keeping this serializable answer intact. Because I read it's faster and I just want to keep everything parcelable across the app. Please also read my last comment about the issue, I can `putParcelableArrayListExtra` in activityA but can't `getParcelableArrayListExtra` in activityB due to compilation error. – AppDeveloper Mar 06 '20 at 06:00
  • you mean `Bundle.putSerializable`? https://stackoverflow.com/questions/14333449/passing-data-through-intent-using-serializable – AppDeveloper Mar 06 '20 at 06:27
  • Yes Bundle sorry :-) FYI "Parceleable is faster than Serializable" I wouldn't worry about that at all, it's nanoseconds unless you have huge objects that are constantly being parceled. Get it working, then optimize for performance if you have to (which I bet you won;t) – Blundell Mar 06 '20 at 08:42