Problems passing integer and serializable on bundl

2019-09-01 14:24发布

问题:

I'm trying to pass a serializable and an Integer or a String but on my child activity I only get the serializable

Parent Activity

Intent intent = new Intent(Class1.this, Class2.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 23);
bundle.putSerializable(serializable, object);
intent.putExtras(bundle);
startActivityForResult(intent, 1);

Child Activity

Intent intent = getIntent();
int intKey;
Bundle bundle = intent.getExtras();
object = (Object) bundle.getSerializable(serializable);
intKey = bundle.getInt("key", 0);

I get the serializable object but I can't get the integer aswell

回答1:

Try and put the int directly on Intent and not through bundle. Meaning :

intent.putExtra("key", 23);

And get it with :

intent.getIntExtra("key", 23);

Should work

But if you have a reason you wanna use the bundle you can try what Sajmon said in the comment, and bundle.getInt("key") i read somewhere that it's not the same, i'm not sure why.