Kotlin for Android Development Tutorial: Android Fundamentals with Activities, Fragments, Lifecycle, ViewBinding, and RecyclerView


This Kotlin for Android Development tutorial introduces Android fundamentals including Android Studio setup, Activities, Fragments, lifecycle management, ViewBinding, and RecyclerView. The chapter focuses on modern Android development practices using Kotlin, providing clean examples and best practices to help developers build scalable and maintainable Android applications.

Kotlin for Android Development – Android Fundamentals (Complete Tutorial)

Android Studio Setup

Steps

  1. Download Android Studio from the official Android website
  2. Install with default settings
  3. Select Kotlin as the primary language
  4. Use Gradle (Kotlin DSL recommended)

Best Practices

  1. Always use the latest stable Android Studio
  2. Enable auto-import and Kotlin formatting
  3. Use Gradle Kotlin DSL for consistency

Activities and Fragments

Activity

An Activity represents a single screen in an Android app.


class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Fragment

Fragments represent reusable UI components.


class HomeFragment : Fragment(R.layout.fragment_home)

Best Practices

  1. Keep Activities lightweight
  2. Move UI logic into Fragments
  3. Use single-activity architecture

Lifecycle Management

Android components have a lifecycle managed by the system.

Key Lifecycle Methods

  1. onCreate
  2. onStart
  3. onResume
  4. onPause
  5. onStop
  6. onDestroy

override fun onStart() {
super.onStart()
Log.d("Lifecycle", "onStart called")
}

Best Practices

  1. Release resources in onStop
  2. Observe lifecycle using LifecycleObserver
  3. Avoid memory leaks

ViewBinding

ViewBinding provides type-safe access to views.

Enable ViewBinding


android {
buildFeatures {
viewBinding = true
}
}

Usage Example


private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.textView.text = "Hello Kotlin"
}

Best Practices

  1. Prefer ViewBinding over findViewById
  2. Clear binding reference in Fragments

RecyclerView

RecyclerView efficiently displays large data sets.

Adapter Example


class ItemAdapter(private val items: List<String>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

class ViewHolder(val binding: ItemRowBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ItemRowBinding.inflate(
LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.textView.text = items[position]
}

override fun getItemCount() = items.size
}

Best Practices

  1. Use ListAdapter with DiffUtil
  2. Avoid heavy logic in onBindViewHolder
  3. Use ViewHolder pattern properly

Real-World Example: Simple List Screen

  1. Activity hosts Fragment
  2. Fragment displays RecyclerView
  3. ViewBinding used for UI
  4. Lifecycle-aware data loading

Chapter Summary

This chapter covered Android fundamentals using Kotlin, including Android Studio setup, Activities, Fragments, lifecycle management, ViewBinding, and RecyclerView. These concepts form the foundation of modern Android app development with Kotlin.