Problems passing integer and serializable on bundle

主宰稳场 提交于 2019-12-24 20:09:21

问题


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.



来源:https://stackoverflow.com/questions/10693007/problems-passing-integer-and-serializable-on-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!