2

Say we have to code a custom View for a toggle button.

A toggle button typically has these two characteristics: - Is clickcable (i.e. will somehow report click events) - Has a state (on/off)

Where should we put the piece of code that makes the toggle button switch state whenever clicked? Does it belong to the custom View itself? Should the View be totally 'dumb' instead and just report clicks letting the business logic set its on/off state instead (e.g. via a setState() API on the View). What are the pros and cons of both approaches?

Let's assume in our codebase we want to strive to let the business logic handle the application state as much as possible and let the Views just handle their "rendering": How the answers to the questions above will change in this respect?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Marco Romano
  • 1,169
  • 7
  • 24

1 Answers1

0

Where should we put the piece of code that makes the toggle button switch state whenever clicked?

A Widget runs on the home screen of the device, so the business logic needs to be called with a PendingIntent

here you can find a valid explanation, and also this blog gives some working examples, even more complex

Clickable widgets in android

trocchietto
  • 2,607
  • 2
  • 23
  • 36
  • I was using the term "UI widget" in its general (not Android) meaning. So that in android meant "View" (or class that extends android.view.View). I will try to change the question title accordingly. – Marco Romano Nov 13 '17 at 09:17
  • sure, let me know if is going to work with a Pending Intent. – trocchietto Nov 13 '17 at 11:47