A Standalone Approach to Threads

by Veli Tasalı   ·   Feb. 25, 2019

experiment ideas

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other

Running a second thread on Android is a tricky task. You may encounter crashes or funny looking GUI widgets when it is not managed correctly. We can stop it when the activity is recreated, but sometimes, we want it to complete in the background. If you are looking for this kind of scenario and want to reattach threads to the activities, this can help you do so.

protected void onDestroy() {

In the example above, getDefaultInterrupter() returns the class level instance of the Interrupter class notifying threads when they are interrupted. This is similar to Thread.interrupt(), but for more threads.

An Interrupter has two modes; user requests and any other request. A user request happens when the user (or something under the influence of the user) invokes the interrupter.

There is also the Closer class invoked by Interrupter. You can use this to release resources when the interrupter they belong to gets a signal for cancellation.

We will not need Interrupter for single task activities because they will only have one task to manage, similar to a music player having only one thread for showing the info for the playing song. However, deleting files or moving them is a type of task that can happen with multiple instances.

Activities that handle multiple tasks need a different approach. They need to own the threads in some way to have them back. We are going to use intents for that. Intents have different signatures and we can use this to refer to activities. You can take image viewers as an example. The activity showing an image will have the URI for it. We can show a text whenever the same intent data starts the activity. The simplest way could be to hash it. This example will return the same integer for the same intent:

public static int intentHash(@NonNull Intent intent)
    StringBuilder builder = new StringBuilder();


    if (intent.getExtras() != null)

    return builder.toString().hashCode();

Now is time to attach and detach the activity. We will give threads nullable objects. This will ensure the safety of view-like elements outside of activities. We will also have a central thread manager which is a service in this case.

The thread manager will provide tasks with application context and resources (e.g. texts). It will also start and keep track of them.

Assume that we have a method similar to this:

public RunningTask findTaskByHash(int hashCode) {
    synchronized (mTaskList) {
       for (RunningTask runningTask : getTaskList())
           if (runningTask.hashCode() == hashCode)
               return runningTask;
    return null;

This will return a task matching the hash code. We can access the task manager service by binding it to a context.

Series Understanding the Architecture of TrebleShot

This post is part of a series. This section shows other posts in the same series.

TrebleShot Wiki

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other
A Standalone Approach to Threads

by Veli Tasalı ·  Feb. 25, 2019

A Guide to Developing TrebleShot Based Applications

by Veli Tasalı ·  July 9, 2019