inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Apr 11 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
Launching Android Activities in a Separate Task

Henrique Boregio wrote this on September 09, 2014 under android, inaka .

Launching Android Activities in a Separate Task

When designing apps for our customers, we often run into a scenario where we want to launch different workflows from the same app, and have it behave independently from our main task. For example, let's imagine that one of the features of our app is to allow the user to create a blog post. To make this a more obvious example, let's imagine that the process of creating such post from within the app is divided into different activities:

  • Activity 1: Write a title
  • Activity 2: Write a description
  • Activity 3: Upload an image
  • Activity 4: Add category tags

As you can see, this is a pretty straightforward flow with a very specific goal, so what happens when the user successfully completes this flow and wants to go back to the main app? In our case, we want this flow to run separately from the app and upon completion, return seamlessly to the main app. If you've ever had this sort of scenario before, you might have noticed that the actual problem we are facing could be summarized as such: "Suppose the user is currently at Activity 4, how can we close all of the past activities (1, 2, 3 and 4) and return to the original activity which triggered this flow?".

There are actually several ways to accomplish this, but one of the simpler ones is to use what is known as Affinity. An affinity is simply a way to indicate which task an activity prefers to belong to (by default, all activities in an application will share the same affinity, meaning they will prefer to live inside the same task). So, we can think of it as a way to group activities together. In our case, we could say that these 4 activities all belong to a "Blog Post Creation" affinity.

So how do we go about setting up our code to reflect this? It's actually quite simple. All we have to do is modify our AndroidManifest.xml file and set the android:taskAffinity tag in our activities. Following with our example, we must make each Activity share the same taskAffinity, like so:

<activity
  android:name=".Activity1"
  android:taskAffinity="com.company.app.create_blog_post">
</activity>

So now that we've defined that these 4 activities all share the same affinity, all that's left to do is define how to close all of them whenever the user has reached the end of the blog post creation process, which in our case happens after they've entered some category tags in Activity 4. How do we accomplish this? We do it simply by calling the method finishAffinity() belonging to the Activity class. As the Javadoc says, this method: "Finishes this activity as well as all activities immediately below it in the current task that have the same affinity.". In other words, if we call this method from Activity4, all activities (1, 2, 3 and 4) will be finished since they all share the same affinity.

Conclusion

What we've seen here is just a hypothetical use case of when to make use of taskAffinity. There are of course several other scenarios where we need to conceptually separate parts of our apps into their own sections (tasks), and this feature provides a very elegant solution that saves us from having to manually deal with tasks, activity creation/termination/resume from within our code.