/kaptain

👨‍✈️ multi-module navigation on Android has never been so easier!

Primary LanguageKotlinMIT LicenseMIT

JitPack Github Actions Android API Kotlin ktlint License MIT

Kaptain

Kaptain is a small, dependencyless and easy to use Android library that helps you to navigate between activities spread over several modules.

Usage

Given the following project structure:

/app
  MyApplication.kt
/feature-a
  FeatureAActivity.kt
/feature-b
  FeatureBActivity.kt
/feature-shared
  Destination.kt
  • app module imports all modules below
  • feature-a and feature-b imports only feature-shared
  • feature-shared imports nothing

1. Define destinations

First, you must list all possible destinations (Activities) of your app. Create a sealed class that implements the KaptainDestination interface.

Optionally, you can add arguments to your destination using a data class.

sealed class Destination : KaptainDestination {

    object FeatureA : Destination()
    data class FeatureB(val message: String) : Destination()
}

2. Create a Kaptain instance

Next, create a new Kaptain instance and associate your destinations with the corresponding Activities.

class MyApplication : Application() {
    
    val kaptain = Kaptain {
        add<Destination.FeatureA, FeatureAActivity>()
        add<Destination.FeatureB, FeatureBActivity>()
    }
}

Ideally, you should inject this instance as a singleton using a DI library. Check the sample app for an example using Koin.

3. Navigate between activities

Now you can navigate to any Activity, from any module:

class FeatureAActivity : AppCompatActivity() {

    fun goToFeatureB() {
        kaptain.navigate(
            activity = this,
            destination = Destination.FeatureB(message = "Ahoy!"),
            requestCode = 0x123 // Optional
        )
    }
}

4. Retrieve a destination content

After arrive at your destination, you can retrieve it's content:

class FeatureBActivity : AppCompatActivity() {

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

        val importantMessage = kaptain.fromIntent<Destination.FeatureB>(this)?.message
    }
}

Dynamic feature modules

Kaptain works great with dynamic features!

1. Add destinations on demand

You can add/remove destinations at any time:

kaptain.add<Destination.DynamicFeatureA, DynamicFeatureAActivity>()
kaptain.remove<Destination.DynamicFeatureB>()

2. Make sure the destination exists before navigating

if (kaptain.has<Destination.DynamicFeatureA>) {
    kaptain.navigate(this, Destination.DynamicFeatureA)
}

Import to your project

  1. Add the JitPack repository in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  1. Next, add the library to your module:
dependencies {
    implementation "com.github.adrielcafe.kaptain:kaptain:$currentVersion"
}

Current version: JitPack