1

I am wondering about the following. In the Android documentation they recommend the following:

"Tip: When your configuration Activity first opens, set the Activity result to RESULT_CANCELED, along with EXTRA_APPWIDGET_ID, as shown in step 5 above. This way, if the user backs-out of the Activity before reaching the end, the App Widget host is notified that the configuration was cancelled and the App Widget will not be added."

https://developer.android.com/guide/topics/appwidgets/index.html#Configuring

But isn't that redundant, since the default value is RESULT_CANCELED (0) anways? Am I missing something? Can there be cases where the result is not at 0 when we open the configuration activity?

Florian Walther
  • 6,237
  • 5
  • 46
  • 104
  • It's not so much that the result code needs to be set. It's the result `Intent` that's important. It needs to have the Widget ID attached, or [bad things can happen](https://stackoverflow.com/q/40708973). – Mike M. Mar 13 '18 at 14:05
  • 1
    Oh of course, how could I miss that. Makes total sense! You can add this as an answer as well. – Florian Walther Mar 13 '18 at 14:06

1 Answers1

1

The important part of that statement is "along with EXTRA_APPWIDGET_ID". You're correct that the result code will be RESULT_CANCELED by default, but there isn't going to be a result Intent with the Widget ID attached by default.

Certainly, any launcher that allows Widgets should be able to handle it gracefully if that Intent is not set, but it's a known issue that at least some do not.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thank you. Right now I check for the app widget id and finish the activity if it should be "INVALID_APPWIDGET_ID". So I should set my result value before this part? Or wont the result have any benefit if it doesnt contain a valid ID? – Florian Walther Mar 13 '18 at 14:16
  • I'm not sure if I understand what you're asking. You should set the `RESULT_CANCELED` code and its `Intent` with the incoming Widget ID in `onCreate()`, so if the user backs out, it's already set. – Mike M. Mar 13 '18 at 14:19
  • Oh, yeah, always use the ID you get passed to you. – Mike M. Mar 13 '18 at 14:20
  • What I mean is, what if the appWidgetId I get from getIntent is invalid? Will the launcher still choke if I pass this in the result intent? – Florian Walther Mar 13 '18 at 14:21
  • It really shouldn't, _if_ it passed that to you. I wouldn't think it ever would, though, unless something's gone horribly wrong on its end. – Mike M. Mar 13 '18 at 14:24
  • So it would still make sense to set the result intent with the invalid id, before potentially finishing the activity (because of the invalid id)? – Florian Walther Mar 13 '18 at 14:39
  • Yeah, basically. I mean, there's nothing else you can do about it. You don't really have any control over ID generation or assignment, so all you can do is pass back whatever you get. – Mike M. Mar 13 '18 at 14:43
  • Yea, makes sense – Florian Walther Mar 13 '18 at 14:46
  • I just want to say thank you again because you have been a great help for me today. – Florian Walther Mar 13 '18 at 22:18
  • No problem. Glad to help. Good luck with your project. Cheers! – Mike M. Mar 13 '18 at 22:44