Classes in Kotlin – Part 2

In Part 1 we dealt with the simplest form of initialization with the primary constructor: class Planet( val diameter: Int, val distanceToSun: Double, val mass: Double ) But wait: there is more: Init block The primary constructor cannot contain any code so init block for the rescue: class Planet( val diameter: Int, val distanceToSun: Double,…

Kotlinx Datetime

Dependency Add the following to your dependencies implementation(org.jetbrains.kotlinx:kotlinx-datetime:0.2.0) Usage import kotlinx.datetime.* val today = Clock.System.todayAt(TimeZone.currentSystemDefault()) If you target Android devices running below API 26, you need to use Android Gradle plugin 4.0 or newer and enable core library desugaring. https://github.com/Kotlin/kotlinx-datetime

Log4j2 for Kotlin

Motivation Logging is a common good practice in software engineering. It enables you to monitor applications in production to gather information about crashes and other malfunctions for further analysis. It is the “little brother” of debugging and often a precursor for setting up test cases which can lead to reproducing the bugs on the developers…

Kotlin Scripts with Gradle

If you are using Gradle and you want to run a Kotlin script you have to add the following to your build.gradle file dependencies { implementation “org.jetbrains.kotlin:kotlin-stdlib” implementation ‘org.jetbrains.kotlin:kotlin-script-runtime:1.4.32’ }

New Blog Post

Passing data between AndroidActivities

Sending data private void openVideoActivity(String video) { Intent newActivity = new Intent(this, PlayerActivity.class); newActivity.putExtra(“videoId”, video); startActivity(newActivity); } Retrieving data final String videoID = getIntent().getExtras().getString(“videoID”);   In Kotlin it looks like this: val intent = Intent(this, NewActivity::class.java).apply { putExtra(“EXTRA_MESSAGE”, “Test 123”) } startActivity(intent) Retrieving val message = intent.getStringExtra(“EXTRA_MESSAGE”) Further Reading Passing data between fragments using SafeArgs…

Android App Lifecycle

Code class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Log.d(“LifecycleTest”, “onCreate”) } override fun onStart() { super.onStart() Log.d(“LifecycleTest”, “onStart”) } override fun onResume() { super.onResume() Log.d(“LifecycleTest”, “onResume”) } override fun onPause() { super.onPause() Log.d(“LifecycleTest”, “onPause”) } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) Log.d(“LifecycleTest”, “onSaveInstanceState”) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { Log.d(“LifecycleTest”,…

Learning Kotlin – Part 1 – print

Motivation I wanted to try writing an Android app. In spite of using plain old Java I wanted to give the new kid on the block Kotlin (from the Jetbrains folks) a try. First observations Coming from a Python background it’s good to see that Kotlin doesn’t use semi-colons! But the curly braces from Java…