summaryrefslogtreecommitdiff
path: root/api-guidelines/methods.md
blob: 14ad03a9dc0fdb2232e4fa7d98df5400b1d731ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
## Methods [M] <a name="methods"></a>

These are rules about various specifics in methods, around parameters, method
names, return types, and access specifiers.

### Time <a name="time"></a>

#### Prefer `java.time.*` types where possible <a name="time-types"></a>

`java.time.Duration`, `java.time.Instant` and many other `java.time.*` types are
available on all platform versions through
[desugaring](https://developer.android.com/studio/write/java8-support-table) and
should be preferred when expressing time in API parameters or return values.

Libraries targeting SDK < 26 *must not* use `java.time.*` until the AAR format
supports advertising core library desugaring requirements (see
[b/203113147](https://issuetracker.google.com/203113147)).

Prefer exposing only variants of an API that accept or return
`java.time.Duration` or `java.time.Instant` and omit primitive variants with the
same functionality unless the API domain is one where object allocation in
intended usage patterns would have a prohibitive performance impact.

#### Methods expressing durations should be named duration <a name="time-durations"></a>

If a time value expresses the duration of time involved, name the parameter
“duration”, not “time”.

```java {.bad}
ValueAnimator.setTime(java.time.Duration);
```

```java {.good}
ValueAnimator.setDuration(java.time.Duration);
```

**Exceptions:**

“timeout” is appropriate when the duration specifically applies to a timeout
value.

“time” with a type of `java.time.Instant` is appropriate when referring to a
specific point in time, not a duration.

#### Methods expressing durations or time as a primitive should be named with their time unit, and use `long` <a name="long-durations"></a>

Methods accepting or returning durations as a primitive should suffix the method
name with the associated time units (e.g. `Millis`, `Nanos`, `Seconds`) to
reserve the undecorated name for use with `java.time.Duration`. See
[Time](#java-time-types).

Methods should also be annotated apporiately with their unit and time base:

-   `@CurrentTimeMillisLong`: Value is a non-negative timestamp measured as the
    number of milliseconds since 1970-01-01T00:00:00Z.
-   `@CurrentTimeSecondsLong`: Value is a non-negative timestamp measured as the
    number of seconds since 1970-01-01T00:00:00Z.
-   `@DurationMillisLong`: Value is a non-negative duration in milliseconds.
-   `@ElapsedRealtimeLong`: Value is a non-negative timestamp in the
    `SystemClock.elapsedRealtime()` time base.
-   `@UptimeMillisLong`: Value is a non-negative timestamp in
    the`SystemClock.uptimeMillis()` time base.

Primitive time parameters or return values should use `long`, not `int`.

```java {.bad}
ValueAnimator.setDuration(@DurationMillisLong long);
```

```java {.good}
ValueAnimator.setDurationNanos(long);
```

#### Methods expressing units of time should prefer non-abbreviated shorthand for unit names <a name="time-abbreviations"></a>

```java {.bad}
public void setIntervalNs(long intervalNs);

public void setTimeoutUs(long timeoutUs);
```

```java {.good}
public void setIntervalNanos(long intervalNanos);

public void setTimeoutMicros(long timeoutMicros);
```

#### Annotate `long` time arguments <a name="time-annotations"></a>

The platform includes several annotations to provide stronger typing for
`long`-type time units:

*   `@CurrentTimeMillisLong`: Value is a non-negative timestamp measured as the
    number of milliseconds since `1970-01-01T00:00:00Z`, e.g. in the
    `System.currentTimeMillis()` time base.
*   `@CurrentTimeSecondsLong`: Value is a non-negative timestamp measured as the
    number of seconds since `1970-01-01T00:00:00Z`.
*   `@DurationMillisLong`: Value is a non-negative duration in milliseconds.
*   `@ElapsedRealtimeLong`: Value is a non-negative timestamp in the
    `SystemClock#elapsedRealtime()` time base.
*   `@UptimeMillisLong`: Value is a non-negative timestamp in the
    `SystemClock#uptimeMillis()` time base.

### Units of measurement <a name="measurement"></a>

For all methods expressing a unit of measurement *other* than time, prefer
CamelCased
[SI unit prefixes](https://en.wikipedia.org/wiki/International_System_of_Units#Units_and_prefixes).

```java {.good}
public  long[] getFrequenciesKhz();

public  float getStreamVolumeDb();
```

### Put optional parameters at end of overloads <a name="optional-params-last"></a>

If you have overloads of a method with optional parameters, keep those
parameters at the end and keep consistent ordering with the other parameters:

```java {.bad}
public int doFoo(boolean flag);

public int doFoo(int id, boolean flag);
```

```java {.good}
public int doFoo(boolean flag);

public int doFoo(boolean flag, int id);
```

When adding overloads for optional arguments, the behavior of the simpler
methods should behave in exactly the same way as if default arguments had been
provided to the more elaborate methods.

Corollary: Don’t overload methods other than to add optional arguments or to
accept different types of arguments if the method is polymorphic. If the
overloaded method does something fundamentally different, then give it a new
name.

Note: The guideline on placement of [single abstract method](#sam-types)
parameters (ex. `Runnable`, listeners) overrides this guideline. In cases where
a developer could reasonably expected to write the body of a SAM class as a
lambda, the SAM class parameter should be placed last.

Note: The guideline on [use of Executors](#provide-executor) overrides this
guideline, as it allows for an overload that omits an `Executor`, even though it
is not the final argument in the parameter list.

### Methods with default parameters must be annotated with `@JvmOverloads` (Kotlin only) <a name="default-value-jvmoverloads"></a>

Methods and constructors with default parameters must be annotated with
`@JvmOverloads` to ensure they maintain binary compatibility.

See
[Function overloads for defaults](https://developer.android.com/kotlin/interop#function_overloads_for_defaults)
in the official Kotlin-Java interop guide for more details.

```kotlin {.good}
class Greeting @JvmOverloads constructor(
  loudness: Int = 5
) {
  @JvmOverloads
  fun sayHello(prefix: String = "Dr.", name: String) = // ...
}
```

### Do not remove default parameter values (Kotlin only) <a name="default-value-removal"></a>

If a method has shipped with a parameter with a default value, removal of the
default value is a source-breaking change.

### The most distinctive and identifying method parameters should be first <a name="distinctive-params-first"></a>

If you have a method with multiple parameters, put the most relevant ones first.
Parameters that specify flags and other options are less important than those
that describe the object that is being acted upon. If there is a completion
callback, put it last.

```java {.bad}
public void openFile(int flags, String name);

public void openFileAsync(OnFileOpenedListener listener, String name, int flags);

public void setFlags(int mask, int flags);
```

```java {.good}
public void openFile(String name, int flags);

public void openFileAsync(String name, int flags, OnFileOpenedListener listener);

public void setFlags(int flags, int mask);
```

See also: [Put optional parameters at end in overloads](#optional-params-last)

### Builders <a name="builders"></a>

The Builder pattern is recommended for creating complex Java objects, and is
commonly used in Android for cases where:

-   The resulting object's properties should be immutable
-   There are a large number of required properties, e.g. many constructor
    arguments
-   There is a complex relationship between properties at construction time,
    e.g. a verification step is required. Note that this level of complexity
    often indicates problems with the API's usability.

Kotlin-sourced classes should prefer `@JvmOverloads`-annotated constructors with
default arguments over Builders, but may choose to improve usabilty for Java
clients by also providing Builders in the cases outlined above.

```kotlin {.good}
class Tone @JvmOverloads constructor(
  val duration: Long = 1000,
  val frequency: Int = 2600,
  val dtmfConfigs: List<DtmfConfig> = emptyList()
) {
  class Builder {
    // ...
  }
}
```

#### Builder classes *must* return the builder <a name="builders-return-builder"></a>

Builder classes must enable method chaining by returning the Builder object
(e.g. `this`) from every method except `build()`. Additional built objects
should be passed as arguments -- do not return a different object’s builder. For
example:

```java {.bad}
public static class Builder {
  public void setDuration(long);
  public void setFrequency(int);
  public DtmfConfigBuilder addDtmfConfig();
  public Tone build();
}
```

```java {.good}
public class Tone {
  public static class Builder {
    public Builder setDuration(long);
    public Builder setFrequency(int);
    public Builder addDtmfConfig(DtmfConfig);
    public Tone build();
  }
}
```

In rare cases where a base builder class must support extension, use a generic
return type:

```java {.good}
public abstract class Builder<T extends Builder<T>> {
  abstract T setValue(int);
}

public class TypeBuilder<T extends TypeBuilder<T>> extends Builder<T> {
  T setValue(int);
  T setTypeSpecificValue(long);
}
```

#### Builder classes *must* be created through a constructor <a name="builder-constructor"></a>

To ensure consistent builder creation through Android API surface, all the
builders *must* be created through a constructor and not a static creator
method.

```java {.bad}
public class Tone {
  public static Builder builder();
  public static class Builder {
  }
}
```

```java {.good}
public class Tone {
  public static class Builder {
    public Builder();
  }
}
```

#### All arguments to builder constructors *must* be required (e.g. `@NonNull`) <a name="builders-nonnull-constructors"></a>

Optional, e.g. `@Nullable`, arguments should be moved to setter methods. The
builder constructor should throw an `NullPointerException` (consider using
`Objects.requireNonNull`) if any required arguments are not specified.

#### Builder classes *should* be final static inner classes of their built types <a name="builders-static-inner"></a>

For the sake of logical organization within a package, builder classes should
typically be exposed as final inner classes of their built types, ex.
`Tone.Builder` rather than `ToneBuilder`.

#### Builders *may* include a constructor to create a new instance from an existing instance <a name="builders-copy"></a>

Builders *may* include a copy constructor to create a new builder instance from
an existing builder or built object. They *should not* provide alternative
methods for creating builder instances from existing builders or build objects.

```java {.bad}
public class Tone {
  public static class Builder {
    public Builder clone();
  }

  public Builder toBuilder();
}
```

```java {.good}
public class Tone {
  public static class Builder {
    public Builder(Builder original);
    public Builder(Tone original);
  }
}
```

##### Builder setters *should* take `@Nullable` arguments if the builder has copy constructor <a name="builders-copy-nullable-setters"></a>

Resetting is essential if a new instance of a builder may be created from an
existing instance. If no copy constructor is available, then the builder may
have either `@Nullable` or `@NonNullable` arguments.

```java {.good}
public static class Builder {
  public Builder(Builder original);
  public Builder setObjectValue(@Nullable Object value);
}
```

##### Builder setters *may* take `@Nullable` arguments for optional properties <a name="builders-optional-nullable-setters"></a>

It's often simpler to use a nullable value for second-degree input, especially
in Kotlin, which utilizes default arguments instead of builders and overloads.

Additionally, `@Nullable` setters will match them with their getters, which must
be `@Nullable` for optional properties.

```java {.bad}
Value createValue(@Nullable OptionalValue optionalValue) {
  Value.Builder builder = new Value.Builder();
  if (optionalValue != null) {
    builder.setOptionalValue(optionalValue);
  }
  return builder.build();
}
```

```java {.good}
Value createValue(@Nullable OptionalValue optionalValue) {
  return new Value.Builder()
    .setOptionalValue(optionalValue);
    .build();
}

// Or in other cases:

Value createValue() {
  return new Value.Builder()
    .setOptionalValue(condition ? new OptionalValue() : null);
    .build();
}
```

Common usage in Kotlin:

```kotlin {.bad}
fun createValue(optionalValue: OptionalValue? = null) =
  Value.Builder()
    .apply { optionalValue?.let { setOptionalValue(it) } }
    .build()
```

```kotlin {.good}
fun createValue(optionalValue: OptionalValue? = null) =
  Value.Builder()
    .setOptionalValue(optionalValue)
    .build()
```

The default value (if the setter is not called), and the meaning of `null`, must
be properlty documented in both the setter and the getter.

```java
/**
 * ...
 *
 * <p>Defaults to {@code null}, which means the optional value will not be used.
 */
```

#### Builder setters *may* be provided for mutable properties where setters are available on the build class <a name="builders-mutable-setters"></a>

If your class has mutable properties and needs a `Builder` class, first ask
yourself whether your class should *actually* have mutable properties.

Next, if you're certain that you need mutable properties, decide which of the
following scenarios works better for your expected use case:

1.  The built object should be immediately usable, thus setters *should* be
    provided for *all* relevant properties whether mutable or immutable.

    ```java {.good}
    map.put(key, new Value.Builder(requiredValue)
        .setImmutableProperty(immutableValue)
        .setUsefulMutableProperty(usefulValue)
        .build());
    ```

2.  Some additional calls may need to be made before the built object can be
    useful, thus setters *should not* be provided for mutable properties.

    ```java {.good}
    Value v = new Value.Builder(requiredValue)
        .setImmutableProperty(immutableValue)
        .build();
    v.setUsefulMutableProperty(usefulValue)
    Result r = v.performSomeAction();
    Key k = callSomeMethod(r);
    map.put(k, v);
    ```

Don't mix the two scenarios.

```java {.bad}
Value v = new Value.Builder(requiredValue)
    .setImmutableProperty(immutableValue)
    .setUsefulMutableProperty(usefulValue)
    .build();
Result r = v.performSomeAction();
Key k = callSomeMethod(r);
map.put(k, v);
```

#### Builders *should* not have getters <a name="getter-on-builder"></a>

Getter should be on the built object, not the builder.

#### Builder setters *must* have corresponding getters on the built class <a name="builders-symmetric-setters"></a>

```java {.bad}
public class Tone {
  public static class Builder {
    public Builder setDuration(long);
    public Builder setFrequency(int);
    public Builder addDtmfConfig(DtmfConfig);
    public Tone build();
  }
}
```

```java {.good}
public class Tone {
  public static class Builder {
    public Builder setDuration(long);
    public Builder setFrequency(int);
    public Builder addDtmfConfig(DtmfConfig);
    public Tone build();
  }

  public long getDuration();
  public int getFrequency();
  public @NonNull List<DtmfConfig> getDtmfConfigs();
}
```

#### Builder classes are expected to declare a build() method <a name="builder-must-declare-build"></a>

#### Builder method naming <a name="builder-method-naming"></a>

Builder methods names should use `setFoo()` / `addFoo()` / `clearFoo()` style.

#### Builder `build()` methods must return `@NonNull` objects <a name="builder-non-null-build"></a>

A builder's `build()` method is expected to return a non-null instance of the
constructed object. In the event that the object cannot be created due to
invalid parameters, validation can be deferred to the build method and 
an `IllegalStateException` should be thrown.

### Do not expose internal locks <a name="avoid-synchronized"></a>

Methods in the public API should not use the `synchronized` keyword. This
keyword causes your object/class to be used as the lock, and since it’s exposed
to others, you may encounter unexpected side effects if other code outside your
class starts using it for locking purposes.

Instead, perform any required locking against an internal, private object.

```java {.bad}
public synchronized void doThing() { ... }
```

```java {.good}
private final Object mThingLock = new Object();

public void doThing() {
  synchronized (mThingLock) {
    ...
  }
}
```

### Use `is` prefix for boolean accessor methods <a name="boolean-methods"></a>

This is the standard naming convention for boolean methods and fields in Java.
Generally, boolean method and variable names should be written as questions that
are answered by the return value.

Java boolean accessor methods should follow a `set`/`is` naming scheme and
fields should prefer `is`, as in:

```java {.good}
// Visibility is a direct property. The object "is" visible:
void setVisible(boolean visible);
boolean isVisible();

// Factory reset protection is an indirect property.
void setFactoryResetProtectionEnabled(boolean enabled);
boolean isFactoryResetProtectionEnabled();

final boolean isAvailable;
```

Using `set`/`is` for Java accessor methods or `is` for Java fields will allow
them to be used as properties from Kotlin:

```kotlin
obj.isVisible = true
obj.isFactoryResetProtectionEnabled = false
if (!obj.isAvailable) return
```

Properties and accessor methods should generally use positive naming, e.g.
`Enabled` rather than `Disabled`. Using negative terminology inverts the meaning
of `true` and `false` and makes it more difficult to reason about behavior.

```java {.bad}
// Passing false here is a double-negative.
void setFactoryResetProtectionDisabled(boolean disabled);
```

In cases where the boolean describes inclusion or ownership of a property, you
may use *has* rather than *is*; however, this will *not* work with Kotlin
property syntax:

```java {.good}
// Transient state is an indirect property used to track state
// related to the object. The object is not transient; rather,
// the object "has" transient state associated with it:
void setHasTransientState(boolean hasTransientState);
boolean hasTransientState();
```

Some alternative prefixes that may be more suitable include *can* and *should*:

```java {.good}
// "Can" describes a behavior that the object may provide,
// and here is more concise than setRecordingEnabled or
// setRecordingAllowed. The object "can" record:
void setCanRecord(boolean canRecord);
boolean canRecord();

// "Should" describes a hint or property that is not strictly
// enforced, and here is more explicit than setFitWidthEnabled.
// The object "should" fit width:
void setShouldFitWidth(boolean shouldFitWidth);
boolean shouldFitWidth();
```

Methods that toggle behaviors or features may use the *is* prefix and *Enabled*
suffix:

```java {.good}
// "Enabled" describes the availability of a property, and is
// more appropriate here than "can use" or "should use" the
// property:
void setWiFiRoamingSettingEnabled(boolean enabled)
boolean isWiFiRoamingSettingEnabled()
```

Generally, method names should be written as questions that are answered by the
return value.

#### Kotlin property methods <a name="boolean-methods-kotlin"></a>

For a class property `var foo: Foo` Kotlin will autogenerate `get`/`set` methods
using a simple rule: prepend `get` and uppercase the first character for the
getter, and prepend `set` and uppercase the first character for the setter. The
above declaration will produce methods named `public Foo getFoo()` and `public
void setFoo(Foo foo)`, respectively.

If the property is of type `Boolean` an additional rule applies in name
generation: if the property name begins with `is`, then `get` is not prepended
for the getter method name, the property name itself is used as the getter.
Therefore, **prefer naming `Boolean` properties with an `is` prefix** in order
to follow the naming guideline above:

```kotlin {.good}
var isVisible: Boolean
```

If your property is one of the aforementioned exceptions and begins with an
appropriate prefix, use the `@get:JvmName` annotation on the property to
manually specify the appropriate name:

```kotlin {.good}
@get:JvmName("hasTransientState")
var hasTransientState: Boolean

@get:JvmName("canRecord")
var canRecord: Boolean

@get:JvmName("shouldFitWidth")
var shouldFitWidth: Boolean
```

### Bitmask accessors <a name="bitmask-accessors"></a>

See [Use `@IntDef` for bitmask flags](#annotations-intdef-bitmask) for API
guidelines regarding defining bitmask flags.

#### Setters <a name="bitmask-accessors-setters"></a>

Two setter methods should be provided: one that takes a full bitstring and
overwrites all existing flags and another that takes a custom bitmask to allow
more flexibility.

```java {.good}
/**
 * Sets the state of all scroll indicators.
 * <p>
 * See {@link #setScrollIndicators(int, int)} for usage information.
 *
 * @param indicators a bitmask of indicators that should be enabled, or
 *                   {@code 0} to disable all indicators
 * @see #setScrollIndicators(int, int)
 * @see #getScrollIndicators()
 */
public void setScrollIndicators(@ScrollIndicators int indicators);

/**
 * Sets the state of the scroll indicators specified by the mask. To change
 * all scroll indicators at once, see {@link #setScrollIndicators(int)}.
 * <p>
 * When a scroll indicator is enabled, it will be displayed if the view
 * can scroll in the direction of the indicator.
 * <p>
 * Multiple indicator types may be enabled or disabled by passing the
 * logical OR of the desired types. If multiple types are specified, they
 * will all be set to the same enabled state.
 * <p>
 * For example, to enable the top scroll indicator:
 * {@code setScrollIndicators(SCROLL_INDICATOR_TOP, SCROLL_INDICATOR_TOP)}
 * <p>
 * To disable the top scroll indicator:
 * {@code setScrollIndicators(0, SCROLL_INDICATOR_TOP)}
 *
 * @param indicators a bitmask of values to set; may be a single flag,
 *                   the logical OR of multiple flags, or 0 to clear
 * @param mask a bitmask indicating which indicator flags to modify
 * @see #setScrollIndicators(int)
 * @see #getScrollIndicators()
 */
public void setScrollIndicators(@ScrollIndicators int indicators, @ScrollIndicators int mask);
```

#### Getters <a name="bitmask-accessors-getters"></a>

One getter should be provided to obtain the full bitmask.

```
/**
 * Returns a bitmask representing the enabled scroll indicators.
 * <p>
 * For example, if the top and left scroll indicators are enabled and all
 * other indicators are disabled, the return value will be
 * {@code View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_LEFT}.
 * <p>
 * To check whether the bottom scroll indicator is enabled, use the value
 * of {@code (getScrollIndicators() & View.SCROLL_INDICATOR_BOTTOM) != 0}.
 *
 * @return a bitmask representing the enabled scroll indicators
 */
@ScrollIndicators
public int getScrollIndicators();
```

### Use `public` instead of `protected` <a name="avoid-protected"></a>

Always prefer `public` to `protected` in public API. Protected access ends up
being painful in the long run, because implementers have to override to
implement the functionality in cases where external access would have been just
as good.

Remember that `protected` visibility **does not** prevent developers from
calling an API -- it only makes it slightly more obnoxious.

### Implement neither or both of `equals()` and `hashCode()` <a name="equals-and-hashcode"></a>

If you override one, you must override the other.

### Implement `toString()` for data classes <a name="toString"></a>

Data classes are encouraged to override `toString()`, to help developers debug
their code.

#### Document whether the output is for program behavior or debugging <a name="tostring-document-debug"></a>

Decide whether you want program behavior to rely on your implementation or not.
For example,
[UUID.toString()](https://developer.android.com/reference/java/util/UUID#toString\(\))
and
[File.toString()](https://developer.android.com/reference/java/io/File#toString\(\))
document their specific format for programs to use. If you are exposing
information for debugging only, like
[Intent](https://developer.android.com/reference/android/content/Intent#toString\(\)),
simply inherit docs from the superclass.

#### Do not include extra information <a name="tostring-no-extra"></a>

All the information available from `toString()` should also be available through
the public API of the object. Otherwise, you are encouraging developers to parse
and rely on your `toString()` output, which will prevent future changes. A good
practice is to implement `toString()` using only the object's public API.

#### Discourage reliance on debug output <a name="defensive-format"></a>

While it's impossible to *prevent* developers from depending on debug output,
including the `System.identityHashCode` of your object in its `toString()`
output will make it very unlikely that two different objects will have equal
`toString()` output.

```java
@Override
public String toString() {
  return getClass().getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(this)) + " {mFoo=" + mFoo + "}";
}
```

This can effectively discourage developers from writing test assertions like
`assertThat(a.toString()).isEqualTo(b.toString())` on your objects.

### Use `createFoo` when returning newly created objects <a name="create-methods"></a>

Use the prefix `create`, not `get` or `new`, for methods that will create return
values, e.g. by constructing new objects.

When the method will create an object to return, make that clear in the method
name.

```java {.bad}
public FooThing getFooThing() {
  return new FooThing();
}
```

```java {.good}
public FooThing createFooThing() {
  return new FooThing();
}
```

### Methods accepting `File` objects should also accept streams <a name="files-and-streams"></a>

Data storage locations on Android are not always files on disk. For example,
content passed across user boundaries is represented as `content://` `Uri`s. To
enable processing of various data sources, APIs which accept `File` objects
should also accept `InputStream` and/or `OutputStream`.

```java {.good}
public void setDataSource(File file)
public void setDataSource(InputStream stream)
```

### Take and return raw primitives instead of boxed versions <a name="auto-boxing"></a>

If you need to communicate missing or null values, consider using `-1`,
`Integer.MAX_VALUE`, or `Integer.MIN_VALUE`.

```java {.bad}
public java.lang.Integer getLength()
public void setLength(java.lang.Integer)
```

```java {.good}
public int getLength()
public void setLength(int value)
```

Avoiding class equivalents of primitive types avoids the memory overhead of
these classes, method access to values, and, more importantly, autoboxing that
comes from casting between primitive and object types. Avoiding these behaviors
saves on memory and on temporary allocations that can lead to expensive and more
frequent garbage collections.

### Use annotations to clarify valid parameter and return values <a name="annotations"></a>

Developer annotations were added to help clarify allowable values in various
situations. This makes it easier for tools to help developers when they supply
incorrect values (for example, passing an arbitrary `int` when the framework
requires one of a specific set of constant values). Use any and all of the
following annotations when appropriate:

**Important:** The invariants specified by the annotations are not automatically
asserted at runtime and must be manually checked. The annotations are only an
indication to developers and used by the documentation generator and Android
Lint.

#### Nullability <a name="annotations-nullability"></a>

Explicit nullabilty annotations are required for Java APIs, but the concept of
nullability is part of the Kotlin language and nullability annotations should
never be used in Kotlin APIs.

**`@Nullable`**: Indicates that a given return value, parameter, or field can be
null:

```java {.good}
@Nullable
public String getName()

public void setName(@Nullable String name)
```

**`@NonNull`**: Indicates that a given return value, parameter, or field
*cannot* be null. Marking things as `@Nullable` is relatively new to Android, so
most of Android's API methods are not consistently documented. Therefore we have
a tri-state of "unknown, `@Nullable`, `@NonNull`" which is why `@NonNull` is
part of the API guidelines.:

```java {.good}
@NonNull
public String getName()

public void setName(@NonNull String name)
```

For Android platform docs, annotating your method parameters will automatically
generate documentation in the form "This value may be null." unless "null" is
explicitly used elsewhere in the parameter doc.

**Existing “not really nullable” methods:** Existing methods in the API without
a declared `@Nullable` annotation may be annotated `@Nullable` if the method can
return `null` under specific, obvious circumstances (e.g. `findViewById()`).
Companion `@NotNull requireFoo()` methods that throw `IllegalArgumentException`
should be added for developers who do not want to null check.

##### Nullability enforcement { #nullability }

In Java, methods are **recommended** to perform input validation for `@NonNull`
parameters via
[`Objects.requireNonNull()`](https://developer.android.com/reference/java/util/Objects.html#requireNonNull\(T,%20java.lang.String\))
and throw a `NullPointerException` when the parameters are null. This is
automatically performed in Kotlin.

#### Resources <a name="annotations-resources"></a>

**Resource identifiers**: Integer parameters that denote ids for specific
resources should be annotated with the appropriate resource-type definition.
There is an annotation for every type of resource, such as `@StringRes`,
`@ColorRes`, and `@AnimRes`, in addition to the catch-all `@AnyRes`. For
example:

```java {.good}
public void setTitle(@StringRes int resId)
```

#### `@IntDef` for constant sets <a name="annotations-intdef"></a>

**Magic constants**: `String` and `int` parameters that are meant to receive one
of a finite set of possible values denoted by public constants should be
annotated appropriately with `@StringDef` or `@IntDef`. These annotations allow
you to create a new annotation that you can use that works like a typedef for
allowable parameters. For example:

```java {.good}
/** @hide */
@IntDef(prefix = {“NAVIGATION_MODE_”}, value = {
  NAVIGATION_MODE_STANDARD,
  NAVIGATION_MODE_LIST,
  NAVIGATION_MODE_TABS
})
@Retention(RetentionPolicy.SOURCE)
public @interface NavigationMode {}

public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;

@NavigationMode
public int getNavigationMode();
public void setNavigationMode(@NavigationMode int mode);
```

Notice that the constants must be defined in the class that will use them, not
in a subclass or interface.

Methods are **recommended** to check the validity of the annotated parameters
and throw an `IllegalArgumentException` if the parameter is not part of the
`@IntDef`

#### `@IntDef` for bitmask flags <a name="annotations-intdef-bitmask"></a>

The annotation can also specify that the constants are flags, and can be
combined with &amp; and I:

```java {.good}
/** @hide */
@IntDef(flag = true, prefix = { “FLAG_” }, value = {
  FLAG_USE_LOGO,
  FLAG_SHOW_HOME,
  FLAG_HOME_AS_UP,
});
@Retention(RetentionPolicy.SOURCE)
public @interface DisplayOptions {}
```

#### `@StringDef` for string constant sets <a name="annotations-stringdef"></a>

There is also the `@StringDef` annotation, which is exactly like `@IntDef`
above, but for `String` constants. You can include multiple “prefix” values
which are used to automatically emit documentation for all
values.

#### `@SdkConstant` for SDK constants <a name="annotations-sdkconstant"></a>

**@SdkConstant** Annotate public fields when they are one of these `SdkConstant`
values: `ACTIVITY_INTENT_ACTION`, `BROADCAST_INTENT_ACTION`, `SERVICE_ACTION`,
`INTENT_CATEGORY`, `FEATURE`.

```java {.good}
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
public static final String ACTION_CALL = "android.intent.action.CALL";
```

### Ensure overrides have compatible nullability <a name="annotations-nullability-overrides"></a>

To ensure API compatibility, the nullability of overrides should be compatible
with the current nullability of the parent. The table below represents the
compatibility expectations. Plainly, overrides should only be as restrictive or
more restrictive than the element they override.

type         | parent      | child
------------ | ----------- | -----------------------
return type  | unannotated | unannotated \| non-null
return type  | nullable    | nullable \| non-null
return type  | non-null    | non-null
             |             |
fun argument | unannotated | unannotated \| nullable
fun argument | nullable    | nullable
fun argument | non-null    | nullable \| non-null

### Prefer non-Nullable (e.g. `@NonNull`) arguments where possible <a name="prefer-nonnull-arguments"></a>

When methods are overloaded, prefer that all arguments are non-`null`.

```java {.good}
public void startActivity(@NonNull Component component) { ... }
public void startActivity(@NonNull Component component, @NonNull Bundle options) { ... }
```

This rule applies to overloaded property setters as well. The primary argument
should be non-`null` and clearing the property should be implemented as a
separate method. This prevents "nonsense" calls where the developer must set
trailing parameters even though they are not required.

```java {.bad}
public void setTitleItem(@Nullable IconCompat icon, @ImageMode mode)
public void setTitleItem(@Nullable IconCompat icon, @ImageMode mode, boolean isLoading)

// Nonsense call to clear property
setTitleItem(null, MODE_RAW, false);
```

```java {.good}
public void setTitleItem(@NonNull IconCompat icon, @ImageMode mode)
public void setTitleItem(@NonNull IconCompat icon, @ImageMode mode, boolean isLoading)
public void clearTitleItem()
```

### Prefer non-`Nullable` (e.g. `@NonNull`) return types for containers <a name="prefer-nonnull-return"></a>

For container types -- `Bundle`s, `Collection`s, etc. -- return an empty (and
immutable, where applicable) container. In cases where `null` would be used to
distinguish availability of a container, consider providing a separate `boolean`
method.

```java {.good}
@NonNull
public Bundle getExtras() { ... }
```

Note: `Intent.getExtras()` returns a `@Nullable` Bundle and specifies a case
where it returns `null`, but this was a mistake that should be avoided in future
APIs.

### Nullability annotations for `get`/`set` pairs must agree <a name="symmetric-nullability"></a>

Get/set method pairs for a single logical property should always agree in their
nullability annotations. Failing to follow this guideline will defeat Kotlin's
property syntax, and adding disagreeing nullability annotations to existing
property methods is therefore a source-breaking change for Kotlin users.

```java {.good}
@NonNull
public Bundle getExtras() { ... }
public void setExtras(@NonNull Bundle bundle) { ... }
```

### Return value in failure / error conditions <a name="return-error"></a>

All APIs should permit applications to react to errors. Returning `false`, `-1`,
`null`, or other catch-all values of "something went wrong" do not tell a
developer enough about the failure to set user expectations or accurately track
reliability of their app in the field. When designing an API, imagine that you
are building an application. If you encounter an error, does the API give you
enough information to surface it to the user or react appropriately?

1.  It's fine (and encouraged) to include detailed information in an exception
    message, but developers shouldn't have to parse it to handle the error
    appropriately. Verbose error codes or other information should be exposed as
    methods.
1.  Make sure your chosen error handling option gives you the flexibility to
    introduce new error types in the future. For `@IntDef`, that means including
    an `OTHER` or `UNKNOWN` value - when returning a new code, you can check the
    caller's `targetSdkVersion` to avoid returning an error code the application
    doesn't know about. For exceptions, have a common superclass that your
    exceptions implement, so that any code that handles that type will also
    catch and handle subtypes.
1.  It should be difficult or impossible for a developer to accidentally ignore
    an error -- if your error is communicated by returning a value, annotate
    your method with `@CheckResult`.

Prefer throwing a `? extends RuntimeException` when a failure or error condition
is reached due to something that the developer did wrong, for example ignoring
constraints on input parameters or failing to check observable state.

Setter or action (ex. `perform`) methods may return an integer status code if
the action may fail as a result of asynchronously-updated state or conditions
outside the developer’s control.

Status codes should be defined on the containing class as `public static final`
fields, prefixed with `ERROR_`, and enumerated in an `@hide` `@IntDef`
annotation.

### Method names should always begin with the verb, not the subject <a name="method-name-verb"></a>

The name of the method should always begin with the verb (e.g. `get`, `create`,
`reload`, etc.), not the object you’re acting on.

```java {.bad}
public void tableReload() {
  mTable.reload();
}
```

```java {.good}
public void reloadTable() {
  mTable.reload();
}
```

### Prefer `Collection<T>` types over arrays as return or parameter type <a name="methods-prefer-collection-over-array"></a>

Generically-typed collection interfaces provide several advantages over arrays,
including stronger API guarantees around uniqueness and ordering, support for
generics, and a number of developer-friendly convenience methods.

#### Exception for primitives

If the elements are primitives, *do* prefer arrays instead, in order to avoid
the cost of auto-boxing. See
[Take and return raw primitives instead of boxed versions](#raw-primitives)

#### Exception for performance-sensitive code

In certain scenarios, where the API is used in performance-sensitive code (like
graphics or other measure/layout/draw APIs), it is ok to use arrays instead of
collections in order to reduce allocations and memory churn.

#### Exception for Kotlin

Kotlin arrays are invariant and the Kotlin language provides ample utility APIs
around arrays, so arrays are on-par with `List` and `Collection` for Kotlin APIs
intended to be accessed from Kotlin.

### Prefer `@NonNull` collections <a name="methods-prefer-non-null-collections"></a>

Always prefer `@NonNull` for collection objects. When returning an empty
collection, use the appropriate `Collections.empty` method to return a low-cost,
correctly-typed, and immutable collection object.

Where type annotations are supported, always prefer `@NonNull` for collection
elements.

### Collection mutability <a name="methods-collections-mutability"></a>

Kotlin APIs should prefer read-only (e.g. not `Mutable`) return types for
collections by default *unless* the API contract specifically requires a mutable
return type.

Java APIs, however, should prefer *mutable* return types by default since the
Android platform's implementation of Java APIs does not yet provide a convenient
implementation of immutable collections. The exception to this rule is
`Collections.empty` return types, which are immutable. In cases where mutability
could be exploited by clients -- on purpose or by mistake -- to break the API's
intended usage pattern, Java APIs should return a shallow copy of the
collection.

```java {.bad}
@Nullable
public PermissionInfo[] getGrantedPermissions() {
  return mPermissions;
}
```

```java {.good}
@NonNull
public Set<PermissionInfo> getGrantedPermissions() {
  if (mPermissions == null) {
    return Collections.emptySet();
  }
  return new ArraySet<>(mPermissions);
}
```

#### Explicitly mutable return types <a name="methods-collections-mutability-return"></a>

APIs that return collections should ideally not modify the returned collection
object after returning. If the returned collection must change or be reused in
some way, for example, an adapted view of a mutable data set, the precise
behavior of when the contents of a previously returned collection can change
must be documented and/or appeal to an appropriate established convention. For
example:

```kotlin {.good}
/**
 * Returns a view of this object as a list of [Item]s.
 */
fun MyObject.asList(): List<Item> = MyObjectListWrapper(this)
```

The Kotlin `.asFoo()` convention is described
[below](#kotlin-conversion-functions) and permits the collection returned by
`.asList()` to change if the original collection changes.

### Use of `vararg` parameter type <a name="methods-vararg"></a>

Both Kotlin and Java APIs are encouraged to use `vararg` in cases where the
developer would be likely to create an array at the call site for the sole
purpose of passing multiple, related parameters of the same type.

```java {.bad}
public void setFeatures(Feature[] features) { ... }

// Developer code
setFeatures(new Feature[]{Features.A, Features.B, Features.C});
```

```java {.good}
public void setFeatures(Feature... features) { ... }

// Developer code
setFeatures(Features.A, Features.B, Features.C);
```

#### Defensive copies <a name="methods-vararg-copies"></a>

Both Java and Kotlin implementations of `vararg` parameters compile to the same
array-backed bytecode and as a result may be called from Java code with a
mutable array. API designers are *strongly encouraged* to create a defensive
shallow copy of the array parameter in cases where it will be persisted to a
field or anonymous inner class.

```java {.good}
public void setValues(SomeObject... values) {
   this.values = Arrays.copyOf(values, values.length);
}
```

Note that creating a defensive copy does not provide any protection against
concurrent modification between the initial method call and the creation of the
copy, nor does it protect against mutation of the objects contained in the
array.

### Provide correct semantics with collection type parameters / returned types <a name="type-semantics"></a>

`List<Foo>` is default option, but consider other types to provide additional
meaning:

*   Use `Set<Foo>`, if your API is indifferent to the order of elements and it
    doesn’t allow duplicates or duplicates are meaningless.

*   `Collection<Foo>,` if your API is indifferent to the order and allows
    duplicates.

Note: Remember that Java Collections are mutable by default, so consider
defensive copying for your return and parameter types. Another option for the
return type is `Collection.unmodifiable*`.

### Kotlin conversion functions <a name="kotlin-conversion-functions"></a>

Kotlin frequently uses `.toFoo()` and `.asFoo()` to obtain an object of a
different type from an existing object where `Foo` is the name of the
conversion's return type. This is consistent with the familiar JDK
`Object.toString()`. Kotlin takes this further by using it for primitive
conversions such as `25.toFloat()`.

The distinction between conversions named `.toFoo()` and `.asFoo()` is
significant:

#### Use `.toFoo()` when creating a new, independent object {.numbered}

Like `.toString()`, a "to" conversion returns a new, independent object. If the
original object is modified later, the new object will not reflect those
changes. Similarly, if the *new* object is modified later, the *old* object will
not reflect those changes.

```kotlin {.good}
fun Foo.toBundle(): Bundle = Bundle().apply {
    putInt(FOO_VALUE_KEY, value)
}
```

#### Use `.asFoo()` when creating a dependent wrapper, decorated object, or cast {.numbered}

Casting in Kotlin is performed using the `as` keyword. It reflects a change in
*interface* but not a change in *identity.* When used as a prefix in an
extension function, `.asFoo()` decorates the receiver. A mutation in the
original receiver object will be reflected in the object returned by `asFoo()`.
A mutation in the new `Foo` object *may* be reflected in the original object.

```kotlin {.good}
fun <T> Flow<T>.asLiveData(): LiveData<T> = liveData {
    collect {
        emit(it)
    }
}
```

#### Conversion functions should be written as extensions {.numbered}

Writing conversion functions outside of both the receiver and the result class
definitions reduces coupling between types. An ideal conversion needs only
public API access to the original object. This proves by example that a
developer can write analogous conversions to their own preferred types as well.

#### Throw appropriate specific exceptions <a name="appropriate-exception"></a>

Methods must not throw generic exceptions such as `java.lang.Exception` or
`java.lang.Throwable`, instead an appropriate specific exception has to be used
like `java.lang.NullPointerException` to allow developers to handle exceptions
without being overly broad.

Errors that are unrelated to the arguments provided directly to the publicly
invoked method should throw `java.lang.IllegalStateException` instead of
`java.lang.IllegalArgumentException` or `java.lang.NullPointerException`.

## Listeners and Callbacks <a name="callbacks"></a>

These are the rules around the classes and methods used for listener/callback
mechanisms.

### Callback class names should be singular <a name="callback-class-singular"></a>

Use `MyObjectCallback` instead of `MyObjectCallbacks`.

### Callback method names should be of the format `on<Something>` <a name="callback-method-naming"></a>

`onFooEvent` signifies that `FooEvent` is happening and that the callback should
act in response.

### Past vs. present tense should describe timing behavior <a name="callback-tense"></a>

Callback methods regarding events should be named to indicate whether the event
has already happened or is in the process of happening.

For example, if the method is called after a click action has been performed:

```java
public void onClicked()
```

However, if the method is responsible for performing the click action:

```java
public boolean onClick()
```

### Registering/unregistering callbacks <a name="registering-unregistering-callbacks"></a>

When a listener or callback can be added or removed from an object, the
associated methods should be named add/remove OR register/unregister. Be
consistent with the existing convention used by the class or by other classes in
the same package. When no such precedent exists, prefer add/remove.

Methods involving registering or unregistering callbacks should specify the
whole name of the callback type.

```java {.good}
public void addFooCallback(@NonNull FooCallback callback);
public void removeFooCallback(@NonNull FooCallback callback);
```

```java {.good}
public void registerFooCallback(@NonNull FooCallback callback);
public void unregisterFooCallback(@NonNull FooCallback callback);
```

#### Avoid getters for callbacks <a name="avoid-callback-getters"></a>

Do not add `getFooCallback()` methods. This is a tempting escape hatch for cases
where developers may want to chain an existing callback together with their own
replacement, but it is brittle and makes the current state difficult to reason
about for component developers. For example,

*   Developer A calls `setFooCallback(a)`
*   Developer B calls `setFooCallback(new B(getFooCallback()))`
*   Developer A wishes to remove its callback `a` and has no way to do so
    without knowledge of `B`’s type, and `B` having been built to allow such
    modifications of its wrapped callback.

### Accept Executors to control callback dispatch <a name="provide-executor"></a>

When registering callbacks that have no explicit threading expectations (pretty
much anywhere outside the UI toolkit), it is strongly encouraged to include an
`Executor` parameter as part of registration to allow the developer to specify
the thread upon which the callbacks will be invoked.

```java {.good}
public void registerFooCallback(
    @NonNull @CallbackExecutor Executor executor,
    @NonNull FooCallback callback)
```

Note: Developers ***must*** provide a valid `Executor`. The new
`@CallbackExecutor` annotation will add automatic documentation to tell
developers about common default options. Also note that the callback argument is
required to be last to enable idiomatic usage from Kotlin.

As an exception to our usual
[guidelines about optional parameters](#optional-params-last), it is ok to
provide an overload omitting the `Executor` even though it is not the final
argument in the parameter list. If the `Executor` is not provided, the callback
should be invoked on the main thread using `Looper.getMainLooper()` and this
should be documented on the associated overloaded method.

```java {.good}
/**
 * ...
 * Note that the callback will be executed on the main thread using
 * {@link Looper.getMainLooper()}. To specify the execution thread, use
 * {@link registerFooCallback(Executor, FooCallback)}.
 * ...
 */
public void registerFooCallback(
    @NonNull FooCallback callback)

public void registerFooCallback(
    @NonNull @CallbackExecutor Executor executor,
    @NonNull FooCallback callback)
```

**`Executor` implementation gotchas:** Note that the following is a valid
executor!

```java
public class SynchronousExecutor implements Executor {
    @Override
    public void execute(Runnable r) {
        r.run();
    }
}
```

This means that when implementing APIs that take this form, your incoming binder
object implementation on the app process side **must** call
`Binder.clearCallingIdentity()` before invoking the app’s callback on the
app-supplied Executor. This way any application code that uses Binder identity
(e.g. `Binder.getCallingUid()`) for permission checks correctly attributes the
code running to the application and not to the system process calling into the
app. If users of your API want the UID / PID information of the caller then this
should be an explicit part of your API surface, rather than implicit based on
where the Executor they supplied ran.

The above **should** be supported by your API. In performance-critical cases
apps may need to run code either immediately or synchronously with feedback from
your API. Accepting an Executor permits this. Defensively creating an additional
HandlerThread or similar to trampoline from defeats this desirable use case.

If an app is going to run expensive code somewhere in their own process, **let
them**. The workarounds that app developers will find to overcome your
restrictions will be much harder to support in the long term.

**Exception for single callback**: when the nature of the events being reported
calls for only supporting a single callback instance, use the following style:

```java {.good}
public void setFooCallback(
    @NonNull @CallbackExecutor Executor executor,
    @NonNull FooCallback callback)

public void clearFooCallback()
```

#### Why not `Handler` instead of `Executor`?

Android's `Handler` was used as a standard for redirecting callback execution to
a specific `Looper` thread in the past. This standard was changed to prefer
`Executor` as most app developers manage their own thread pools, making the
main or UI thread the only `Looper` thread available to the app. Use `Executor`
to give developers the control they need to reuse their existing/preferred
execution contexts.

Modern concurrency libraries like kotlinx.coroutines or RxJava provide their own
scheduling mechanisms that perform their own dispatch when needed, which makes
it important to provide the ability to use a direct executor (e.g.
`Runnable::run`) to avoid latency from double thread hops. (e.g. one hop to post
to a `Looper` thread via a `Handler` followed by another hop from the app's
concurrency framework.)

Exceptions to this guideline are rare. Common appeals for an exception include:

**I have to use a `Looper` because I need a `Looper` to `epoll` for the event.**
This exception request is granted as the benefits of `Executor` described above
cannot be realized in this situation.

**I do not want app code to block my thread publishing the event.** This
exception request is typically **not** granted for code that runs in an app
process. Apps that get this wrong are only hurting themselves, not impacting
overall system health. Apps that get it right or use a common concurrency
framework should not pay additional latency penalties.

**`Handler` is locally consistent with other similar APIs in the same class.**
This exception request is granted **situationally.** Preference is for
`Executor`-based overloads to be added, migrating `Handler` implementations to
use the new `Executor` implementation. (`myHandler::post` is a valid
`Executor`!) Depending on the size of the class, number of existing `Handler`
methods, and likelihood that developers would need to use existing `Handler`
based methods alongside the new method, an exception may be granted to add a
new `Handler`-based method.

### Symmetry in Registration <a name="callbacks-symmetry"></a>

If there is a way to add or register something, there should also be a way to
remove/unregister it. The method

```java
registerThing(Thing)
```

should have a matching

```java
unregisterThing(Thing)
```

### Multiple-method callback objects <a name="multi-method-callbacks"></a>

Multiple-method callbacks should prefer `interface` and use `default` methods
when adding to previously-released interfaces. Previously, this guideline
recommended `abstract class` due to the lack of `default` methods in Java 7.

```java {.good}
public interface MostlyOptionalCallback {
  void onImportantAction();
  default void onOptionalInformation() {
    // Empty stub, this method is optional.
  }
}
```

NOTE: The Eclipse guide to
[Evolving Java-based APIs](https://wiki.eclipse.org/Evolving_Java-based_APIs_2)
cautions against using `default` methods in cases where multiple inheritance is
likely; however, this is rare for callbacks and in practice we have only seen
method naming collisions on commonly-extended classes like `Activity`.

### Use `android.os.OutcomeReceiver` when modeling a non-blocking function call

[`OutcomeReceiver<R,E>`](https://developer.android.com/reference/android/os/OutcomeReceiver)
reports a result value `R` when successful or `E : Throwable` otherwise - the
same things a plain method call can do. Use `OutcomeReceiver` as the callback
type when converting a blocking method that returns a result or throws an
exception to a non-blocking async method:

```java
interface FooType {
  // Before:
  public FooResult requestFoo(FooRequest request);

  // After:
  public void requestFooAsync(FooRequest request, Executor executor,
      OutcomeReceiver<FooResult, Throwable> callback);
}
```

Async methods converted in this way always return `void`. Any result that
`requestFoo` would return is instead reported to `requestFooAsync`'s `callback`
parameter's `OutcomeReceiver.onResult` by calling it on the provided `executor`.
Any exception that `requestFoo` would throw is instead reported to the
`OutcomeReceiver.onError` method in the same way.

Using `OutcomeReceiver` for reporting async method results also affords a simple
Kotlin `suspend fun` wrapper for async methods using the
`Continuation.asOutcomeReceiver` extension from `androidx.core:core-ktx`:

```kotlin
suspend fun FooType.requestFoo(request: FooRequest): FooResult =
  suspendCancellableCoroutine { continuation ->
    requestFooAsync(request, Runnable::run, continuation.asOutcomeReceiver())
  }
```

Extensions like the above enable Kotlin clients to call non-blocking async
methods with the convenience of a plain function call without blocking the
calling thread. These 1-1 extensions for platform APIs may be offered as part of
the `androidx.core:core-ktx` artifact in Jetpack when combined with standard
version compatibility checks and considerations. See the documentation for
[asOutcomeReceiver](https://developer.android.com/reference/kotlin/androidx/core/os/package-summary#(kotlin.coroutines.Continuation).asOutcomeReceiver())
for more information, cancellation considerations and samples.

Async methods that do not match the semantics of a method returning a result
or throwing an exception when its work is complete should **not** use
`OutcomeReceiver` as a callback type. Instead consider one of the other options
listed below.

### Prefer functional interfaces over creating new single abstract method (SAM) types <a name="callbacks-sam"></a>

API level 24 added the `java.util.function.*`
([reference docs](https://developer.android.com/reference/java/util/function/package-summary.html))
types, which offer generic SAM interfaces such as `Consumer<T>` that are
suitable for use as callback lambdas. In many cases, creating new SAM interfaces
provides little value in terms of type safety or communicating intent while
unnecessarily expanding the Android API surface area.

Consider using these generic interfaces, rather than creating new ones:

*   `Runnable`: `() -> Unit`
*   `Supplier<R>`: `() -> R`
*   `Consumer<T>`: `(T) -> Unit`
*   `Function<T,R>`: `(T) -> R`
*   `Predicate<T>`: `(T) -> Boolean`
*   [many more available in reference docs](https://developer.android.com/reference/java/util/function/package-summary.html)

#### Placement of SAM parameters

SAM parameters should be placed last to enable idiomatic usage from Kotlin, even
if the method is being overloaded with additional parameters.

```java {.good}
public void schedule(Runnable runnable)

public void schedule(int delay, Runnable runnable)
```