From the course: Android Development Tips
Manage menus with when - Android Tutorial
From the course: Android Development Tips
Manage menus with when
- [Instructor] Kotlin replaces Java's switch statement with the key word When. Both When, and the If keyword, can be used in Kotlin either as a statement or as an expression that returns a value. This ability to use a conditional keyword as an expression makes it easy to evaluate multiple options and execute the appropriate code. So for example, let's say you have an Options menu. In this starting application, I have a menu called Menu Main, that has three options. Just labeled red, green, and blue. In the main activity, I'm loading that menu from On Create Options menu, and then I'm evaluating the selections in On Options Items Selected. This code looks just like the Java version, with a couple of differences. For example, the menu item might be null. So the question mark is added to the type to indicate that possibility. And then the first time that object is referenced there's also a null check. Otherwise, the code looks very similar to what you might get in Java, with an If statement, a couple of Else/Ifs, and an Else. Now, this is a perfect situation where you would use a Switch statement in Java, and in Kotlin, you would use When. And Android Studio will do the work for you. I'll place the cursor after the keyword If. Then I'll use an intention action, and I'll replace If with When. So now I'm evaluating Item ID, which is an integer. And I'm comparing it to three possible values. Resource IDs, and these are defined in the menu. If the comparison is true, then you use the arrow operator and call the resulting code. In this case, I'm calling functions called Handle Red, Handle Green, and Handle Blue. I'll run that code to show that it works as expected. I'll go to the options menu and choose red. And then green, and then blue. But now let's see what the code looks like when you use When as an expression. On Options Items Selected returns a boolean. So I'm going to prefix When with the keyword Return. And then I'll eliminate the keyword Return here. I no longer need the return statement at the bottom. Now I'm saying, return the results of column when as an expression. I pass in the item ID here, and then each of these functions, Handle Red and so on, each return a boolean value as well. I must have an L segment here to make sure that I'm always returning a boolean value. And in fact, I can replace this code with a simple value of false. So now I have incredibly readable and maintainable code. I'm basically saying, when I've evaluated this item ID value, I can then compare it to each of these values and call the resulting code. To make this even more concise, I can now eliminate the creation of the intermediate variable by passing an item comma dot itemid here, and then deleting that line above. I'll run the code again to make sure that nothing is broken. And as expected, I can choose items from the options menu and call the correct code in response. So this is one way of doing conditional logic in Kotlin. You can use both the If keyword and When as expressions rather than as statements. As long as the functions you call satisfy the requirements of your context, in this case I'm inside a function that requires returning a boolean value, and each of these functions returns that value. And now I can clearly see what happens when each menu item is selected.
Contents
-
-
-
(Locked)
Communicate with Snackbar messages6m 15s
-
(Locked)
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
(Locked)
Convert a layout to ConstraintLayout7m 13s
-
(Locked)
Manage global data with singletons5m 37s
-
(Locked)
Convert image files to WEBP format6m 5s
-
(Locked)
Create an Espresso user interface test5m 18s
-
(Locked)
Create classes for Android with Kotlin5m 12s
-
(Locked)
Run background tasks in intent services8m 51s
-
(Locked)
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
(Locked)
Improve forms with design layouts7m 55s
-
(Locked)
Display draggable rows in a list of data8m 1s
-
(Locked)
Manage persistent data with Realm10m 23s
-
(Locked)
Play an audio file from assets8m 56s
-
(Locked)
Send an SMS message from an app7m 56s
-
(Locked)
Get a list of audio files on a device8m 32s
-
(Locked)
Play sounds with SoundPool8m 45s
-
(Locked)
Display the build date of an APK file6m 47s
-
(Locked)
Build Kotlin classes with properties7m 32s
-
(Locked)
Manage versioning with Gradle variables6m 8s
-
(Locked)
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
(Locked)
Make asynchronous calls with Retrofit4m 6s
-
(Locked)
Send request parameters with Retrofit4m
-
(Locked)
Upgrade a project to Android Studio 3.04m 47s
-
(Locked)
Clean up calls to findViewById()2m 58s
-
(Locked)
Create adaptive launcher icons5m 47s
-
(Locked)
Use XML and downloadable fonts6m 40s
-
(Locked)
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
(Locked)
Manage an SQLite database with Room9m 17s
-
(Locked)
Manage background threads with Executors5m 32s
-
(Locked)
Track vital stats in a published app3m 4s
-
(Locked)
Inspect database files with DB Browser4m 41s
-
(Locked)
Execute tasks with JobScheduler7m 29s
-
(Locked)
Create Room entity classes in Kotlin8m 22s
-
(Locked)
Create Room DAO classes in Kotlin4m 13s
-
(Locked)
Upgrade to Android Studio 3.13m 22s
-
(Locked)
Convert a project to use Kotlin3m 20s
-
(Locked)
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
(Locked)
Define global constants with Kotlin6m 12s
-
(Locked)
Use Kotlin data and enum classes5m 24s
-
(Locked)
Create inheritance hierarchies in Kotlin6m 8s
-
(Locked)
Manage data collections in Kotlin5m 45s
-
(Locked)
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
(Locked)
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
(Locked)
Use virtual devices with Google Play3m 50s
-
(Locked)