How to get Date extra from an intent?

爱⌒轻易说出口 提交于 2019-12-24 02:21:03

问题


I am packing an intent and one of the Extras I add is a date object, like this:

intent.putExtra(DATE_EXTRA, t.getDate());

Later, when I reading the extras, I try to get the Date like this:

this.date = new Date(intent.getExtras().getString(DATE_EXTRA));

However, this returns an error about the String being empty. I don't think the above is the right way to do it, because I am not looking for a string, but I have not been able to find an intent.getDateExtra() method anywhere. What do I do?

I know that the date was passed properly, because I can see it while debugging:


回答1:


Replace:

this.date = new Date(intent.getExtras().getString(DATE_EXTRA));

with:

this.date = (Date)intent.getSerializableExtra(DATE_EXTRA);

and see if that helps.




回答2:


Another workaround would be to pass the long value of the date:

intent.putExtra(DATE_EXTRA, t.getDate().getTime());
....
this.date = new Date(intent.getLongExtra(DATE_EXTRA, 0)); //0 is the default value


来源:https://stackoverflow.com/questions/27217280/how-to-get-date-extra-from-an-intent

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