Lecture 3 - Lifecycle

< CS193A Android Programming

Logging

Battery Life

Phone App Paradigm: no Quit

Activity Lifecycle Outline

Some aspects of the lifecycle are tricky, so I want to be careful that we get them right. Much of the behavior you want happens automatically, but you need to understand the underlying sequence.

Here's the broad outline of the various onXXX() "notification" methods called on an activity, signaling its shift from one state to another. There are some other states omitted here, as they do not come up for the common code patterns.

1. Super-Common, No-Kill Case

1a. Add Animation

1b. Make Animation work with onPause/onResume

1c. Common Case Summary

2. The Kill Case

2a. onSaveInstanceState

2c. Kill Case Summary


Got this far in lecture

3. The "State" Case


package edu.stanford.nick;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class LifecycleActivity extends Activity {
	public static final String TAG = "Lifecycle";
	
	// Save mClickCount correctly or not.
	public static final boolean SAVE_CORRECT = false;
	
	private TextView mTextView;
	
	private Button mButton;
	private int mClickCount = 0;
	
	private CheckBox mCheckbox;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "onCreate:" + savedInstanceState);
		super.onCreate(savedInstanceState);
		
		
		if (savedInstanceState!=null && SAVE_CORRECT) {
			mClickCount = savedInstanceState.getInt("mClickCount");
		}
		
		setContentView(R.layout.main);
		
		// Checkbox - Animation
		mCheckbox = (CheckBox) findViewById(R.id.checkBox1);
		mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		    	checkboxToAnimation();
		    }
		});
		// Looks like you maybe want to start the animation here.
		// However, best to do it in onResume() -- "bottleneck" strategy.
		
		
		// Button / clickCount - change the text depending on how many
		// times the button has been clicked (simple example of state to save).
		mTextView = (TextView) findViewById(R.id.textView1);
		mButton = (Button) findViewById(R.id.button1);
		mButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mClickCount++;
				
				if (mClickCount == 1) {
					mTextView.setText("Please do not click this button again.");
				}
				else if (mClickCount >= 2) {
					mTextView.setTextColor(Color.parseColor("#FF0000"));
					// Can store color in res:
					// strings.xml: <color name="mycolor1">#FF0000</color>
					// then: mTextView.setTextColor(R.color.mycolor1);
				}
			}
		});
	}

	@Override
	public void onResume() {
		Log.v(TAG, "onResume");
		super.onResume();
		
		checkboxToAnimation();
	}
	
	/** Looks at the state of the checkbox, and start/stop the animation appropriately. */
	private void checkboxToAnimation() {
		if (mCheckbox.isChecked()) {
			mButton.startAnimation(AnimationUtils.loadAnimation(this, R.anim.pulse));
		}
		else {
			mButton.clearAnimation();
		}
	}
	
	@Override
	public void onPause() {
		Log.v(TAG, "onPause");
		super.onPause();
		
		mButton.clearAnimation();
	}
	
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.v(TAG, "onSaveInstanceState:" + outState);
		super.onSaveInstanceState(outState);
		
		if (SAVE_CORRECT) {
			outState.putInt("mClickCount", mClickCount);
		}
	}


	@Override
	public void onDestroy() {
		Log.v(TAG, "onDestroy");
		super.onDestroy();
	}
}

Homework 3

Add to your existing app or create a new one -- you don't need to turn this in, it's just for practice.