title: Creating a Singleton CountDownTimer in Android
body: <p>I'm a beginner in android and I've written an activity. It contains a CountDownTimer that   counts down from a particular value. It also contains a Button that loads text information   and a textview to display count.    </p>
  
  <p>Below is the code for Activity1:</p>
  
  <pre><code>public class Screen extends Activity1 implements OnClickListener {  
  private static final int MILLIS_PER_SECOND = 1000;  
  private static final int SECONDS_TO_COUNTDOWN = 1;  
  TextView Time;  
  int totaltime;  
  Button startTimer, howTo, pause;  
  protected CountDownTimer MyTimer;  
  int PracticeCount;  
  long tot;  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.pushupscreen);
      getRefs();
      getSpeed();
              getCount();
      setTotalTime();
      startTimer.setOnClickListener(this);
      pause.setOnClickListener(this);
  }
  
  private void getRefs() {
      // Initialize layout resources
      Time = (TextView) findViewById(R.id.tvTime);
  
      startTimer = (Button) findViewById(R.id.bStart);
      howTo = (Button) findViewById(R.id.btHowTo);
      pause = (Button) findViewById(R.id.bPause);
      howTo.setOnClickListener(this);
  
  }
  
    private void getTheCount() { 
   //get count from SharedPreferences
        }
  
  
  
    private void getSpeed() { 
     //get speed from SharedPreferences
            } 
  
   private void setCount(){
          totalTime=speed*count;}
  
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      if (v == startTimer) {
  
          try {
              showTimer(time);
  
          } catch (NumberFormatException e) {
              // method ignores invalid (non-integer) input and waits
              // for something it cant use
          }
      } else if (v == pause) {
          MyTimer.cancel();
          Timer.setText("Resume");
      } else if (v == howTo) {
  
          //Launch screen containing information
      }
  }
  
  private void showTimer(long time) {
      if (MyTimer != null) {
          MyTimer.cancel();
      }
  
      MyTimer = new CountDownTimer(tot2, MILLIS_PER_SECOND) {
          @Override
          public void onTick(long millisUntilFinished) {
              tot = millisUntilFinished;
              long seconds = millisUntilFinished / 1000;
              Time.setText(String.format("%02d", seconds / 60) + ":"
                      + String.format("%02d", seconds % 60));
  
          }
  
          @Override
          public void onFinish() {
              Time.setText("KABOOM!");
  
          }
      }.start();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And here is the layout file for this:<br>
          </p>
  
  <pre><code>&lt;TextView
      android:id="@+id/tvTime"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:gravity="center"
      android:padding="10dip"
      android:text="@string/starttime"
      android:textSize="60sp" /&gt;
  
  &lt;Button
      android:id="@+id/bStart"
      android:layout_width="150dp"
      android:layout_height="wrap_content"
      android:layout_above="@+id/tvTime"
      android:text="Start" /&gt;
  
  &lt;Button
      android:id="@+id/bPause"
      android:layout_width="150dp"
      android:layout_height="wrap_content"
      android:layout_above="@+id/tvTime"
      android:layout_toRightOf="@+id/btHowTo"
      android:text="Pause" /&gt;
  
  &lt;TextView
      android:id="@+id/tvCount"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/btHowTo"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="39dp"
      android:text="25" 
      android:textSize="80sp"
      android:textAlignment="center"/&gt;
  </code></pre>
  
  <p>  </p>
  
  <p>My questions: </p>
  
  <p>1.How do I create 4 activities that use the same layout and the same timer? Each Activity  loads a different content in the textview and a different screen on the click of HowTo button.<br>
  2.How can Activity1 be designed to run for 1/4th the time set and pass remaining time to Activity2? Is it possible?   </p>
  
  <p>I would really appreciate any help and advice that you can provide. Thanks.</p>