(#) Suspicious mix of `setType` and `setData` !!! WARNING: Suspicious mix of `setType` and `setData` This is a warning. Id : `IntentReset` Summary : Suspicious mix of `setType` and `setData` Severity : Warning Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 3.2.0 (September 2018) Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/IntentDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/IntentDetectorTest.kt) Intent provides the following APIs: `setData(Uri)` and `setType(String)`. Unfortunately, setting one clears the other. If you want to set both, you should call `setDataAndType(Uri, String)` instead. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/IntentTest.java:18:Warning: Calling setType after calling setData will clear the data: Call setDataAndType instead? [IntentReset] intent.setType(type); // ERROR 1.1 ------------- src/test/pkg/IntentTest.java:25:Warning: Calling setData after calling setType will clear the type: Call setDataAndType instead? [IntentReset] intent.setData(uri); // ERROR 2.1 ------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/IntentTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import android.content.Intent; import android.net.Uri; public class IntentTest { public void test1() { // OK: Nulls are allowed Intent intent = new Intent(); intent.setData(null); // OK 1 intent.setType(null); // OK 2 } public void test2(Uri uri, String type) { // Error: Cleared Intent intent = new Intent(); intent.setData(uri); // ERROR 1.2 intent.setType(type); // ERROR 1.1 } public void test3(Uri uri, String type) { // Error: Cleared (reverse order from test2) Intent intent = new Intent(); intent.setType(type); // ERROR 2.2 intent.setData(uri); // ERROR 2.1 } public void test4(Uri uri, String type, boolean setData) { // OK: Different code paths Intent intent = new Intent(); if (setData) { intent.setData(uri); // OK 3 } else { intent.setType(type); // OK 4 } if (setData) intent.setData(uri); // OK 5 else intent.setType(type); // OK 6 } public void test5(Uri uri, String type) { // Ok: on different objects Intent intent1 = new Intent(); Intent intent2 = new Intent(); intent1.setData(uri); // OK 7 intent2.setType(type); // OK 8 } public void test6(boolean setData, Uri dataUri, boolean setType, String mime) { Intent intent = new Intent(); if (setData) { intent.setData(dataUri); // OK 9 } if (setType) { intent.setType(mime); // OK 10 } } public void test7(int flavor, Uri dataUri, String mime) { Intent intent = new Intent(); switch (flavor) { case 1: intent.setData(dataUri); // OK 11 break; case 2: intent.setType(mime); // OK 12 break; } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/IntentDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `IntentDetector.testBasic`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("IntentReset") fun method() { Intent(...) } ``` or ```java // Java @SuppressWarnings("IntentReset") void method() { new Intent(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection IntentReset problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="IntentReset" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'IntentReset' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore IntentReset ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).