https://github.com/adrielcafe/kaptain
👨✈️ multi-module navigation on Android has never been so easier!
https://github.com/adrielcafe/kaptain
activity android android-library kotlin kotlin-library module multi-module navigation navigator
Last synced: 6 months ago
JSON representation
👨✈️ multi-module navigation on Android has never been so easier!
- Host: GitHub
- URL: https://github.com/adrielcafe/kaptain
- Owner: adrielcafe
- License: mit
- Created: 2020-03-26T00:47:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T02:29:40.000Z (about 4 years ago)
- Last Synced: 2024-11-07T13:38:28.668Z (12 months ago)
- Topics: activity, android, android-library, kotlin, kotlin-library, module, multi-module, navigation, navigator
- Language: Kotlin
- Homepage:
- Size: 177 KB
- Stars: 27
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- awesome-list - adrielcafe/kaptain - 👨✈️ multi-module navigation on Android has never been so easier! (Kotlin)
README
[](https://jitpack.io/#adrielcafe/kaptain)
[](https://github.com/adrielcafe/kaptain/actions)
[](https://android-arsenal.com/api?level=14)
[](https://kotlinlang.org/)
[](https://ktlint.github.io/)
[](https://opensource.org/licenses/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`.
```kotlin
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.
```kotlin
class MyApplication : Application() {
val kaptain = Kaptain {
add()
add()
}
}
```
Ideally, you should inject this instance as a **singleton** using a DI library. Check the [sample app](https://github.com/adrielcafe/kaptain/blob/master/sample/src/main/java/cafe/adriel/kaptain/sample/App.kt) for an example using [Koin](https://github.com/InsertKoinIO/koin/).
### 3. Navigate between activities
Now you can navigate to any Activity, from any module:
```kotlin
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:
```kotlin
class FeatureBActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_feature_b)
val importantMessage = kaptain.fromIntent(this)?.message
}
}
```
## Dynamic feature modules
Kaptain works great with [dynamic features](https://developer.android.com/guide/app-bundle/dynamic-delivery)!
### 1. Add destinations on demand
You can add/remove destinations at any time:
```kotlin
kaptain.add()
kaptain.remove()
```
### 2. Make sure the destination exists before navigating
```kotlin
if (kaptain.has) {
kaptain.navigate(this, Destination.DynamicFeatureA)
}
```
## Import to your project
1. Add the JitPack repository in your root build.gradle at the end of repositories:
```gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```
2. Next, add the library to your module:
```gradle
dependencies {
implementation "com.github.adrielcafe.kaptain:kaptain:$currentVersion"
}
```
Current version: [](https://jitpack.io/#adrielcafe/kaptain)