A lightweight library for requesting and consuming Activity Results using coroutines.

Overview

Version

SuspendActivityResult

A lightweight library for requesting and consuming Activity Results using coroutines, it's usage is as simple as:

val uri = ActivityResultManager.getInstance().requestResult(
    contract = GetContent(),
    input = "image/*"
)

or using the built-in extensions:

val uri = ActivityResultManager.getInstance().getContent("image/*")

For more information check the articles: Part 1 and Part 2

Runtime Permissions

The library offers a utility class for requesting runtime permissions with access to the shouldShowRequestPermissionRationale's value:

val result = PermissionManager.getInstance()
    .requestPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
when (result) {
    PermissionGranted -> {
        // TODO
    }
    is PermissionDenied -> {
        if (result.shouldShowRationale) {
            // TODO
        } else {
            // TODO
        }
    }
}

This class uses internally ActivityResultManager.getInstance().requestPermission(), so everything below applies to it as well.

Download

implementation 'dev.hichamboushaba.suspendactivityresult:suspendactivityresult:0.1.1'

The default artifact uses App Startup for the initialization.

If you don't want this dependency added, you can use the other variant:

implementation 'dev.hichamboushaba.suspendactivityresult:suspendactivityresult-no-startup:0.1.1'

And initialize the library manually

class App : Application() {
    fun onCreate() {
        super.onCreate()
        ActivityResultManager.init(this)
    }
}

Testing

ActivityResultManager is an interface, so for better testability, it's recommended to inject ActivityResultManager.getInstance() into your components, for easier swapping to a fake or mocked implementation for tests.

Process-death

The implementation of requestResult takes into account process-death scenarios, and keeps track of the pending operation using the Activity's SavedStateRegistry , which means calling requestResult after a process-death, will not re-launch the activity result caller, and instead, it will only register the callback to allow receiving the result. And to make this work as expected, the application need to keep the screen's state across this process-death, to call requestResult afterwards, check the example app for how we can implement this using SavedStateHandle .

You might also like...
๐Ÿ›’ Mercado Libre App Clone using modern Android development with Hilt, Coroutines, Jetpack (Room, ViewModel), and Jetpack Compose based on MVVM architecture.
๐Ÿ›’ Mercado Libre App Clone using modern Android development with Hilt, Coroutines, Jetpack (Room, ViewModel), and Jetpack Compose based on MVVM architecture.

Meli Clone ๐Ÿ›’ Mercado Libre App Clone using modern Android development with Hilt, Coroutines, Jetpack (Room, ViewModel), and Jetpack Compose based on

๐Ÿฆ A Disney app using transformation motions based on MVVM (ViewModel, Coroutines, Flow, LiveData, Room, Repository, Koin) architecture.
๐Ÿฆ A Disney app using transformation motions based on MVVM (ViewModel, Coroutines, Flow, LiveData, Room, Repository, Koin) architecture.

DisneyMotions A demo Disney app using transformation motions based on MVVM architecture. The motion system is included in the 1.2.0-alpha05 released m

An implementation of MediatR on JVM for Spring using Kotlin coroutines

Kpring MediatR In this project, an attempt has been made to implement the mediator pattern on the JVM with simplicity using Kotlin with native corouti

Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

App built using Kotlin, Dagger Hilt, Room Database, Coroutines, Flow, AndroidX Glance, WorkManager, Coil etc.
App built using Kotlin, Dagger Hilt, Room Database, Coroutines, Flow, AndroidX Glance, WorkManager, Coil etc.

An article sharing platform where you can personalize, subscribe to your favorite topics, get daily-read reminders, etc. App built using Kotlin, Dagger Hilt, Room Database, Coroutines, Flow, AndroidX Glance, WorkManager, Coil etc.

Example of using coroutines with Spring MVC

This is an example of using coroutines with Spring MVC (not webflux!) DemoController uses https://github.com/joost-de-vries/spring-coroutine, but sinc

FlowExt is a Kotlin Multiplatform library, that provides many operators and extensions to Kotlin Coroutines Flow

FlowExt | Kotlinx Coroutines Flow Extensions | Kotlinx Coroutines Flow Extensions. Extensions to the Kotlin Flow library | kotlin-flow-extensions | Coroutines Flow Extensions | Kotlin Flow extensions | kotlin flow extensions | Flow extensions

A simple, lightweight and powerful field validation library for Android.
A simple, lightweight and powerful field validation library for Android.

Convalida Convalida - (Italian for "validation") Convalida is a simple, lightweight and powerful field validation library for Android. Documentation G

Kotools Assert is a lightweight assertions library for Kotlin

Kotools Assert is a lightweight assertions library for Kotlin

Owner
Hicham Boushaba
Hicham Boushaba
An Android app that calculates BMI values and results by entering your height and weight

BMICalculator BMI ์ง€์ˆ˜๋Š” ์†Œ์ˆ˜ ํ•œ์ž๋ฆฌ๊นŒ์ง€๋งŒ ์ถœ๋ ฅ๋จ BMI ๊ฒฐ๊ณผ์˜ ์ƒ‰์ƒ์€ ํ•˜๋‹จ ์ด๋ฏธ์ง€๋ทฐ์˜ ์ฒดํ˜•๋ณ„ ๊ฒฐ๊ณผ๊ฐ’์˜ ์ƒ‰์ƒ๊ณผ ๊ฐ™์Œ ์ฐธ๊ณ ํ–ˆ๋˜ ๋งํฌ

HYUNJUNEPARK 0 Feb 10, 2022
A fake shopping app built using the MVVM archirecture consuming FAKE STORE API

FakeStore A fake shopping app built using the MVVM archirecture consuming FAKE STORE API N/B This API does not provide an endpoint for registering use

marsden madoka 4 Aug 17, 2022
An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM pattern and the latest Jetpack components.

StarWars An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM patte

Joel Kanyi 42 Nov 20, 2022
An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern

Images An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern as well as Architecture Components. Min Api

Ronnie Otieno 8 Sep 21, 2022
Simple event library to communicate between Activity/Fragment and ViewModel

Setup dependencies { implementation "com.github.skgmn:viewmodelevent:1.1.0" } If you don't know how to access to GitHub Packges, please refer to

null 4 Apr 6, 2022
Native-Blur: a C++/Kotlin library for blur bitmaps and activity, mobile-ready, android compatible

Native-Blur The Native-Blur is a C++/Kotlin libraray for blur bitmaps and activity, mobile-ready, android compatible, powered by Java Native Interface

Abolfazl Abbasi 26 Dec 13, 2022
A webapp which generates a simple Discord profile banner image in real-time which shows user's status and activity.

DiscordProfileBanner This tool generates a Discord profile banner image in realtime. I wrote it for use in my AniList profile. An example in action: H

Quanta 11 Oct 17, 2022
In this single activity app. i was trying to practice on ViewModel and Livedata

CalwithViewModel In this single activity app. i was trying to practice on ViewModel and Livedata Min Api Level : 19 Setup Requirements Android device

Tech_G 1 Feb 16, 2022
This project demonstrates the usage of Android Activity Recognition Transition API

User Activity Detection This project demonstrates the usage of Android Activity Recognition Transition API. Detect when users start or end an activity

null 0 Dec 18, 2021
Open currenty activity in Android Studio

Open Current Activity Android Studio / IntelliJ Plugin A little plugin for Android development (Android Studio or IntelliJ). Adds an action under Navi

null 0 Apr 24, 2022