Why Bundle in Intent get lost?

Posted: edited May 19 at 11:32 - Source : stackoverflow

I have a Serializable SpecialObject with some parameters (Location etc. in turn Serializable as well)

I would like to open a Fragment (MySpecialFragment) by an Activity (MySpecialActivity) by handling over a SpecialObject object. It works so far, I can trigger and open the MySpecialFragment but the SpecialObject is not handled over properly. I feel like I can't see the forest for the trees.

Debugging it:

val PARAM_BUNDLE = "bundle_extra"
val PARAM_SPECIAL_EXTRA = "special_bundle_extra"

class MySpecialActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my_special)

    val sameIntentSht = intent // same as getIntent()

    val extras = intent.extras // same as getIntent().getExtras()
    val bundleExtras = intent.getBundleExtra(PARAM_BUNDLE)

    val extras2 = sameIntentSht.extras
    val bundleExtras2 = sameIntentSht.getBundleExtra(PARAM_BUNDLE)

    if(extras != null && extras2 != null && bundleExtras != null && bundleExtras2 != null) Timber.d("Jzst for debugging")

    FragmentHelper.inflateFragment(this, R.id.fragmentContainer, MySpecialFragment::class.java, extras)
}

companion object {
    fun createIntent(context: Context,
                     mySpecial: MySpecialObject?): Intent {
        val intent = Intent(context, MySpecialActivity::class.java)
        val bundle = Bundle()
        mySpecial?.let {
            bundle.putSerializable(PARAM_SPECIAL_EXTRA, mySpecial)
        }

        intent.putExtra(PARAM_BUNDLE, bundle)
        // intent.flags = FLAG_ACTIVITY_NEW_TASK
        return intent
    }
}
}

Instantiation createIntent(): Creating the Intent to launch the MySpecialActivity the createIntent() method works fine. When I debug into the intent object it has its bundle and that in turn has the Serializable MySpecialObject object.

Unpacking the Bundle onCreate(): As you can see in the onCreate() all the extra* and bundleExtra* objects don't have mMap

And the getExtras() directly from getIntent() (in Kotlin intent.extras) is not the same extras as sameIntentSht which is obviously the same val sameIntentSht = intent . You see in the debug intent.extras has 652 bytes and sameIntentSht.extras has 604 bytes !??!?

How can I handle over this bundle properly?

EDIT: I used RubenGees suggestion. Works so far:

val intent = Intent(context, MySpecialActivity::class.java)
mySpecial?.let {
    intent.putExtra(PARAM_SPECIAL_EXTRA, mySpecial)
}
// intent.flags = FLAG_ACTIVITY_NEW_TASK
return intent

But on the onCreate I had to copy the intent into sameIntentsht anyway: see here see mMap of extra and extra2 !?