- https://developer.android.com/kotlin/style-guide
- https://kotlinlang.org/docs/coding-conventions.html
./gradlew [module:]dependencies./gradlew assemble[buildVariant]./gradlew install[buildVariant]./gradlew [module:]test[buildVariant]UnitTest./gradlew [module:]connected[buildVariant]AndroidTestThe following custom task in the project-level build.gradle is to delete the project-level build
directory when clicking the menu bar > Build > Clean Project.
task clean(type: Delete) {
delete rootProject.buildDir
}val encoded: String = "<>&'\"".htmlEncode() // <>&'"val decoded: String = String =
Html.fromHtml("<>&'"", Html.FROM_HTML_MODE_COMPACT).toString() // <>&'"String.htmlEncode is a part of the Core KTX library and is syntactic sugar for TextUtils.htmlEncode .
val color: Color = Color.valueOf(0x11223344)/** isOpen and close() require "androidx.drawerlayout:drawerlayout:1.1.0" or higher. */
override fun onBackPressed() {
if (drawerLayout.isOpen) {
drawerLayout.close()
return
}
super.onBackPressed()
}OnBackPressedDispatcher.hasEnabledCallbacks()returns true if both of the following are met.- There is at least one callback registered with this dispatcher.
- Your Activity is being between
onStart()andonStop()(both inclusive). If you overrideonStart()and/oronStop(), it is betweensuper.onStart()andsuper.onStop(). - Even if you pass a Fragment to
OnBackPressedDispatcher.addCallback(...), the Fragment's lifecycle does not affectOnBackPressedDispatcher.hasEnabledCallbacks().
- If your Activity overrides
onBackPressed()but you forget to callsuper.onBackPressed()in it, your callback will never be called.
class MyCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : View(context, attrs, defStyle) {
init {
context.withStyledAttributes(
attrs,
R.styleable.MyCustomView,
defStyle,
0
) { // simpler than Theme.obtainStyledAttributes(...)
// ...
}
}
}