A lightweight Android activity router

Related tags

Kotlin krouter
Overview

Krouter

A lightweight Android activity router written in Kotlin.

crossroads

Basic usage

// anywhere in the app, preferably on application creation
val krouter = Krouter.from(context, hashMapOf(
    "user/:id/likes" to UserLikesActivity::class.java
))

// anywhere that can access the val above
krouter.start("user/42/likes") // this will start UserLikesActivity

// inside UserLikesActivity, possibly inside onCreate()
val id: Int = intent.getIntExtra("id", 0) // 42

Adding more extras

In order to add more extras to the intent, one can do the following:

krouter.getRouter("user/42/likes")!!
    .withIntent { it.putExtra("name", "John") }
    .start()

Starting for result

In order to set a request code:

krouter.getRouter("user/42/likes")!!
    .startForResult(activity, 123) // 123 is the request code

Advanced routing configuration

It is possible to instantiate Krouter establishing a regular expression that the parameter must specify:

val krouter = Krouter(context, hashMapOf(
    Route("user/:id/likes", hashMapOf(
        "id" to Schema("^[0-9]{2}$")
    )) to UserLikesActivity::class.java
)

There are default implementations for integer, float, double, long, char:

// the constants must be statically imported from Schema.Type
Route("user/:id/likes", hashMapOf("id" to Schema(INT)))
Route("my/balance/:value", hashMapOf("value" to Schema(FLOAT)))

If no schema is defined, Krouter will try to infer the type accordingly. If no schema is suitable, the param will be coerced to a String.

Interceptor

You can add some interceptors to Krouter:

// If you are not logged in, go to login
krouter.addInterceptor(object : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Router? {

        val route = chain.request()
        if(route?.url == "user/:id" && !login) {
            val target = chain.proceed(route)
            val router = krouter.getRouter("login")
            router?.intent?.putExtra("nav", target?.url)
            router?.update()
            return router
        }
        return chain.proceed(route)
    }

})

Installation

Add the dependency:

dependencies {
    compile 'com.github.denisidoro.github:krouter:0.0.2'
}

Best practices

  • Use dependency injection: Krouter was idealized to be used in conjunction with Dagger.
  • Compose routers: say you have an activity flow in your app that's only accessible to users who are admin, for instance. If you don't want to deal with a huge routing map that has routes all flows, then create multiple Krouter instances. val globalKrouter and @AdminScope val adminKrouter, for example.
You might also like...
Clickstream - A Modern, Fast, and Lightweight Android Library Ingestion Platform.
Clickstream - A Modern, Fast, and Lightweight Android Library Ingestion Platform.

Clickstream is an event agnostic, real-time data ingestion platform. Clickstream allows apps to maintain a long-running connection to send data in real-time.

:balloon: A lightweight popup like tooltips, fully customizable with an arrow and animations.
:balloon: A lightweight popup like tooltips, fully customizable with an arrow and animations.

Balloon 🎈 A lightweight popup like tooltips, fully customizable with arrow and animations. Including in your project Gradle Add below codes to your r

A lightweight cache library written in Kotlin

[NEW] Released to Maven Central: 'com.github.yundom:kache:1.x.x' Kache A runtime in-memory cache. Installation Put this in your build.gradle implemen

:balloon: A lightweight popup like tooltips, fully customizable with an arrow and animations.
:balloon: A lightweight popup like tooltips, fully customizable with an arrow and animations.

Balloon 🎈 A lightweight popup like tooltips, fully customizable with arrow and animations. Including in your project Gradle Add below codes to your r

Celebrate more with this lightweight confetti particle system 🎊
Celebrate more with this lightweight confetti particle system 🎊

Konfetti 🎊 πŸ₯³ Celebrate more with this lightweight confetti particle system. Create realistic confetti by implementing this easy to use library. Demo

🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru 🚟 Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

Lightweight Kotlin DSL dependency injection library
Lightweight Kotlin DSL dependency injection library

Warehouse DSL Warehouse is a lightweight Kotlin DSL dependency injection library this library has an extremely faster learning curve and more human fr

A fast, lightweight, entity component system library written in Kotlin.

Fleks A fast, lightweight, entity component system library written in Kotlin. Motivation When developing my hobby games using LibGDX, I always used As

Kotlin Object Notation - Lightweight DSL to build fluid JSON trees

Kotlin Object Notation Lightweight kotlin MPP DSL for building JSON trees Setup Just drop the dependency in your commonMain sourceSet kotlin { sourc

Comments
  • Manifest file has allowBackup flag to true

    Manifest file has allowBackup flag to true

    Issue

    Manifest file has set allowBackup flag to true, this is not allowing me to use my application with flag to false. This made me to remove the library. Please revert the flag, or remove it as it is used as a library

    opened by bmgupta 0
Owner
Denis Isidoro
Software Engineer
Denis Isidoro
LifecycleAwareGitHubSearch - The Activity Lifecycle and the ViewModel Architecture

Lifecycle-Aware GitHub Search In this project, we'll modify our GitHub search ap

null 1 Feb 8, 2022
A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.

Pdf Viewer For Android A Simple PDF Viewer library which only occupies around 125kb while most of the Pdf viewer occupies upto 16MB space. How to inte

Rajat 362 Dec 29, 2022
Lightweight data loading and caching library for android

ColdStorage A lightweight data loading and caching library for android Quicklinks Feature requests: Got a new requirement? Request it here and it will

Cryptic Minds 41 Oct 17, 2022
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties Idea Delegated properties in Kotlin allow you to execute a

null 25 Dec 27, 2022
StaticLog - super lightweight static logging for Kotlin, Java and Android

StaticLog StaticLog is a super lightweight logging library implemented in pure Kotlin (https://kotlinlang.org). It is designed to be used in Kotlin, J

Julian Pfeifer 28 Oct 3, 2022
Koi, a lightweight kotlin library for Android Development.

Koi - A lightweight Kotlin library for Android Koi include many useful extensions and functions, they can help reducing the boilerplate code in Androi

Hello World 514 Nov 29, 2022
A lightweight alternative to Android's ViewModels. The easiest way to retain instances in Activities, Fragments or Composables.

A lightweight alternative to Android's ViewModels. The easiest way to retain instances in Activities, Fragments or Composables.

Marcello Galhardo 264 Dec 27, 2022
A lightweight Kotlin friendly wrapper around Couchbase lite for Android.

CouchBaseKtx ?? Work In-Progress ?? A lightweight Kotlin friendly wrapper around Couchbase-lite for Android Read up a little bit of documentation abou

Jaya Surya Thotapalli 5 Feb 15, 2022
A lightweight and simple Kotlin library for deep link handling on Android πŸ”—.

A lightweight and simple Kotlin library for deep link handling on Android ??.

Jeziel Lago 101 Aug 14, 2022
A lightweight, simple, smart and powerful Android routing library.

RxRouter Read this in other languages: δΈ­ζ–‡, English A lightweight, simple, smart and powerful Android routing library. Getting started Setting up the d

Season 323 Nov 10, 2022