About This Solution

This is one way to solve the background threads assignment. It is not the one and only way.

The MainActivity class is where the threads are started and run.

The OddCounter does not show the implementation for the Stretch Challenge, using weak references, but the EvenCounter class does.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="edu.byui.cs.burton.backgroundthreadsactivity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnOddCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:onClick="onOddClick"
        android:text="Count By Odds"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnEvenCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:onClick="onEvenClick"
        android:text="Count by Evens"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btnOddCounter" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

package edu.byui.cs.burton.backgroundthreadsactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onOddClick(View view) {
        OddCounter counter = new OddCounter(this);
        Thread thread = new Thread(counter);

        thread.start();
    }

    public void onEvenClick(View view) {
        // This example does the exact same things as the other event handler
        // but does it all on one line.
        new Thread(new EvenCounter(this)).start();
    }
}

OddCounter.java

package edu.byui.cs.burton.backgroundthreadsactivity;

import android.app.Activity;
import android.widget.Toast;

import java.lang.ref.WeakReference;

/**
 * This class will run on a background thread and count to 100 by odds.
 * For simplicity, it does not use WeakReferences (as you should to do this properly).
 */
public class OddCounter implements Runnable {
    // NOTE: For ease of understanding the basic concepts, this class does not show
    // the WeakReference as defined in the Strech Challenge please see the EvenCounter
    // class for an example of how to use that.
    private Activity activity;

    /**
     * Creates a new OddCounter with the activity that will be used for the Toast
     * at the end.
     * @param activity
     */
    public OddCounter(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void run() {
        // Start at 1 to count by odds
        for (int i = 1; i < 100; i+=2) {
            System.out.println(i);

            try {
                // Simulate a more complex process by sleeping here.
                // In a real program you wouldn't sleep, it just wastes time.
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // we are now finished counting, display the toast:
        // (This could be done with a lambda instead which would be more succinct,
        // but this example shows it will an anonymous class to be more explicit
        // about what's happening.
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // This is the code that will run on the UI thread.
                Toast toast = Toast.makeText(activity, "Odd Counting Finished", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }
}

EvenCounter.java

package edu.byui.cs.burton.backgroundthreadsactivity;

import android.app.Activity;
import android.widget.Toast;

import java.lang.ref.WeakReference;

/**
 * This class is used to count to 100 by even numbers on a background thread.
 * It properly uses weak references.
 */
public class EvenCounter implements Runnable {
    // This variable holds a WeakReference to an activity, so that it will allow
    // it to be garbage collected if necessary. Because of this, whenever we use
    // it, we need to make sure to check if it's null.
    private WeakReference<Activity> activityRef;

    public EvenCounter(Activity activity) {
        this.activityRef = new WeakReference<Activity>(activity);
    }

    @Override
    public void run() {

        // Start at 2 so we count the even numbers
        for (int i = 2; i < 100; i+=2) {
            System.out.println(i);

            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // we are now finished counting, display the toast:

        // first, we'll get the activity from the WeakReference
        final Activity activity = activityRef.get();

        // now, make sure the activity was not destroyed since we last saw it
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // This is the code that will run on the UI thread.
                    Toast toast = Toast.makeText(activity, "Even Counting Finished", Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
        }
    }
}