package co.kuntz.awesomeapplication;

import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends ActionBarActivity {
    public static final String TAG = MainActivity.class.getName();

    private AsyncTask<View, Void, Void> backgroundTask = new AsyncTask<View, Void, Void>() {
        protected Void doInBackground(View... views) {
            while (true) {
                Log.d(TAG, "Changing background color");
                final Random random = new Random();
                final View view = views[0];

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
                        view.refreshDrawableState();
                    }
                });


                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    TextView text = (TextView) getWindow().getDecorView().findViewById(R.id.text);
                    text.setText("Something terrible happened!");
                    break;
                }
            }
            return null;
        }
    };

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

        Log.d(TAG, "Starting application");


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();

        View view = getWindow().getDecorView().findViewById(R.id.main_layout);
        backgroundTask.execute(view);
    }

    @Override
    protected void onPause() {
        super.onPause();
        backgroundTask.cancel(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        backgroundTask.cancel(true);
    }
}

@@ -512,988 +512,10 @@
 Task
- = new AsyncTask%3CView, Void, Void%3E() %7B%0A        protected Void doInBackground(View... views) %7B%0A            while (true) %7B%0A                Log.d(TAG, %22Changing background color%22);%0A                final Random random = new Random();%0A                final View view = views%5B0%5D;%0A%0A                runOnUiThread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));%0A                        view.refreshDrawableState();%0A                    %7D%0A                %7D);%0A%0A%0A                try %7B%0A                    Thread.sleep(2000);%0A                %7D catch (Exception e) %7B%0A                    TextView text = (TextView) getWindow().getDecorView().findViewById(R.id.text);%0A                    text.setText(%22Something terrible happened!%22);%0A                    break;%0A                %7D%0A            %7D%0A            return null;%0A        %7D%0A    %7D
 ;%0A
+
 %0A   
@@ -1609,16 +1609,64 @@
 ayout);%0A
+        backgroundTask = gimmeBackgroundTask();%0A
         
@@ -1880,24 +1880,24 @@
 r.onStop();%0A
-
         back
@@ -1926,11 +1926,1385 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onDestroy() %7B%0A        super.onDestroy();%0A        backgroundTask.cancel(true);%0A    %7D%0A%0A    public AsyncTask%3CView, Void, Void%3E gimmeBackgroundTask() %7B%0A        return new AsyncTask%3CView, Void, Void%3E() %7B%0A            protected Void doInBackground(View... views) %7B%0A                while (true) %7B%0A                    Log.d(TAG, %22Changing background color%22);%0A                    final Random random = new Random();%0A                    final View view = views%5B0%5D;%0A%0A                    runOnUiThread(new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));%0A                            view.refreshDrawableState();%0A                        %7D%0A                    %7D);%0A%0A%0A                    try %7B%0A                        Thread.sleep(2000);%0A                    %7D catch (Exception e) %7B%0A                        //TextView text = (TextView) getWindow().getDecorView().findViewById(R.id.text);%0A                        //text.setText(%22Something terrible happened!%22);%0A                        Log.e(TAG, %22Error changing backgrounds%22, e);%0A                        Log.%0A                        break;%0A                    %7D%0A                %7D%0A                return null;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

Handle activity changes better.