Issue
I'm trying to pass a serializable object that in my widget on click, in my WidgetFactory(extends RemoteViewsService.RemoteViewsFactory) I pass an object:
val fillInIntent = Intent().apply {
putExtra(Constants.Widget.ITEM, item)
putExtra(Constants.IntentItemPosition, 67)
}
itemView.setOnClickFillInIntent(R.id.widgetItemButton, fillInIntent)
return itemView
And in my WidgetProvider in onReceive I get it:
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
when (intent.action) {
Constants.Widget.ACTION-> {
val exampleItem =
intent.getSerializableExtra(Constants.Widget.ITEM) as? ExampleItem
exampleItem!!.let { // some code }
And in the line exampleItem!!.let {} it throws NullPointerException, due to empty extras. In my onUpdate method I set setPendingIntentTemplate:
val intent = Intent(context, WidgetProvider::class.java).apply {
action = Constants.Widget.ACTION putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
}
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT)
widgetViews.setPendingIntentTemplate(R.id.widget_listView, pendingIntent)
I already made logs, to check if extras are empty, they are. But if I comment the line putExtra(Constants.Widget.ITEM, item) and left only putExtra(Constants.IntentItemPosition, 67) I will succesfully get the passed nubmer 67 in my onReceive(if I wouldn't comment the line, I would receive only the default value). I could try to not use putExtra(serializable), and just put strings and integers, but I would really like to pass a serializable object, rather than passing a lot of strings and integers.
Solution
Unfortunately, I couldn't find any solving fot the problem, except of not using "getSerializableExtra" and "putExtra(Serializable)". I decided to put strings and longs/integers instead.
Answered By - MrRuslan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.