// Use seconds, running and wasRunning respectively
// to record the number of seconds passed,
// whether the stopwatch is running and
// whether the stopwatch was running
// before the activity was paused.
// Number of seconds displayed
// on the stopwatch.
private int seconds = 0;
// Is the stopwatch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null) {
// Get the previous state of the stopwatch
// if the activity has been
// destroyed and recreated.
seconds
= savedInstanceState
.getInt("seconds");
running
= savedInstanceState
.getBoolean("running");
wasRunning
= savedInstanceState
.getBoolean("wasRunning");
}
runTimer();
}
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.os.Bundle;
import java.util.Locale;
import android.widget.TextView;
public class StopwatchActivity extends Activity {