Starting a Third-Party activity for result the clean way
Tags: android, cleanarchitecture, kotlin, androidmvp
Published: 2020-05-17 20:11:33

---

published: true
firstPublishedAt: 1505315285010
latestPublishedAt: 1505387313465
slug: starting-a-third-party-activity-for-result-the-clean-way
title: Starting a Third-Party activity for result the clean way
canonical_url: https://medium.com/@jcminarro/starting-a-third-party-activity-for-result-the-clean-way-b2d245cf8a70
tags: Android, Clean Architecture, Kotlin, Android MVP

---

We all know that Android development has been very coupled to its framework. That is because we need the `Context` or the `Activity` to perform several actions. On the last years Android developers have tried to decouple from the framework when using [Clean Architecture](https://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/).

Whenever we use Clean Architecture, we try to wrap every dependency of our project. This way we can replace them easily if we don’t want to depend on them anymore.

For example, when using [MVP](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) in our app, we don’t want the navigation between view to be done in the View layer. The problem is navigation can’t be done by the Presenter layer because a presenter should be pure Java/Kotlin and navigation depends on the Android Framework.

On that case, the typical way to do it is creating a **collaborator.** This is the only component that knows about the Android Framework, and is able to navigate to a new view. By using such collaborator our presenter is decoupled from the Android Framework and no logic is added to our views.

![](https://cdn-images-1.medium.com/max/600/1*ugyAEmDTGL7jOPTcO_oWDQ.gif)

The Android Framework has different ways to invoke actions and obtain a result. One of them is using an `Intent` to start a new activity by calling `startActivityForResult()`. With that, when an activity completes the action, it returns the information and the class that started the action can get it on the `onActivityResult()` method.

There is a problem here: the only classes that are able to get the result are `Activity` and `Fragment` and they should be the view in our Clean Architecture when we use MVP.

That is not a real problem to us because we have a lot of ways to avoid using `startActivityForResult()` and still get the result we wanted. We usually have all the information into our data layer, using a repository to push and pull it. We could start an action that stores some information into our repository and whenever another part of the app needs that information, it would ask the repository for it.

But in some cases, third-party libraries require us to call them through `startActivityForResult()`. That is a very common case when we use a third-party library for images or camera manipulation.

![](https://cdn-images-1.medium.com/max/640/1*tig98lhBNRCG7n1kjwxcoQ.gif)

In this post, we are going to use [Card.IO](https://www.card.io) Paypal’s library as an example. This library can be used to scan and get information from a credit card using our device’s camera. The idea here is to have a collaborator into our presenter that gives us the information of a credit card. Our presenters should be pure Java/Kotlin and we want the presenter’s collaborators to look like pure Java/Kotlin too. We know that at runtime they are going to use whatever Android stuff they need, like opening a new screen that uses the camera SDK and scan the credit card, but this is only an implementation detail. Easy, right?

The first thing that we are going to do is to create an interface that defines a method to start the scan with a callback to receive the card information as a parameter.

{% gist https://gist.github.com/JcMinarro/3eef77b49032d0cce395a24246b421b3 %}

It is going to be used by our presenter in the following way:

{% gist https://gist.github.com/JcMinarro/f90e753ecdbd8f26f360ff3196070d6d %}

So far so good: we have an abstraction that is going to perform the action of getting the information of the card. Now we need to implement it using the Card.IO library. On the Card.IO documentation they show us how to use it, and it is simple: we have to start a new activity for result with an `Intent` in which we ask the information that we would like to receive.

Our implementation of `CardScanner` needs be able to start that activity and gets the result by itself. The only way to do it on Android is from an activity or fragment. We don’t want to have a new screen just to do that. We could do it with a worker fragment without it interacting with our view.

![](https://cdn-images-1.medium.com/max/800/1*nIjvxXGiaZAzF8-VkOKsTQ.gif)

Our Android implementation is going to receive a `FragmentManager` as a constructor parameter. With it, we are able to create as many fragments as we want. As we need to start an activity belonging to the Card.IO library and receive the information of the card, we are going to create a fragment that does just that.
{% gist https://gist.github.com/JcMinarro/4381b9fb67c457aeaec875cfabcbb856 %}

Cool. We have a fragment that is able to start an activity for result and gets the card information from the library. But how are we going to get that information into our Android implementation of the `CardScanner`?

The Android SDK has a class that is used to send information through Inter-Process Communication (IPC), called `ResultReceiver`.

But, on the last API update, they have limited its use, and some methods are now only allowed to be called from the framework. They have added a Lint Warning, and we don’t want to have `@SuppressWarnings` annotations into our code.

This is why I have created another class `ResultReceiver` that has the same implementation, allowing us to send information through IPC. It implements `Parcelable`, so we are able to pass it to our fragment through a bundle, and notify the result whenever we receive the card information.
{% gist https://gist.github.com/JcMinarro/5a3a7bfefef9eafc84cd560cdda04f76 %}

We need to receive that in our `AndroidCardScanner` and send the result through the callback that we stored.
{% gist https://gist.github.com/JcMinarro/36290ceb2980f91e21e36d6e821f3de2 %}

At the full implementation that you can check in the [KleanActivityForResult Repository](https://github.com/JcMinarro/KleanActivityForResult), the collaborator can be called multiple times. That is the reason why whenever `scan()` get called, a new identifier that associates the callback and the fragment is created. Then, when we receive the result, we free the instances.

Perhaps it is not useful on this example, because the user is not going to be able to open it several times. But we could use such approach with an Android service to perform a different action, and in that case, the user is able to call it several times.

And last but not less, this approach allows us to test our app using unit testing. Since we don’t have any Android dependency, we can use jUnit to write black-box tests to check our presentation logic.

First, we mocking our `CardScanner` collaborator to simulate that returns us the card information. After that, we verify the information is rendered properly into or view. The view is going to be an spy which allows us to know the interactions made by our presenter.

{% gist https://gist.github.com/JcMinarro/f7a844b3d2be751789963bc8932680e4 %}

I have used [Kluent](https://markusamshove.github.io/Kluent/) library on the tests. It is a “Fluent Assertions” library written specifically for Kotlin. On the next post I will show you its syntactic sugar to write tests and where is the magic.

Don’t forget to check out the [sample project](https://github.com/JcMinarro/KleanActivityForResult)

Feel free to add me on Twitter [@el_joker333](https://twitter.com/el_joker333) to discuss anything related (or not even related!) to this post.