-1

I'm making an application contains about 20 activities and I want to start a count up timer when the activity 1 starts and finish counting on the last activity. I've found a way to make a subclass and call the timer in 1 activity But I didn't know how to pass the value of the timer from activity 1 to activity 2 and from 2 to 3 . this is my code

subclass

package com.mytimer;

import java.lang.ref.WeakReference;

import java.util.TimerTask;
import android.os.Handler;

import android.widget.TextView;



public class IncrementTask extends TimerTask {

    WeakReference<TextView> mRef;
    int counter = 0;
    Handler handler = new Handler();

    public IncrementTask(TextView text) {
        mRef = new WeakReference<TextView>(text);
    }

    public void run() {
        handler.post(new Runnable() {
            public void run() {
                mRef.get().setText("counter " + counter);
                counter++;
            }
        });
    }
}

in my activity 1

TextView mTextView = (TextView)findViewById(R.id.text);
        Timer timer = new Timer();
        IncrementTask task = new IncrementTask(mTextView);
        timer.scheduleAtFixedRate(task, 0, 1000);

        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent i = new Intent (MainActivity.this, Page2.class);
                startActivity(i);

            }       
        });

I want to know how to pass the value of the timer to the next activity not start the timer from 0 any help please

Sam
  • 69
  • 8
  • u can pass any primitive value using intent.putExtra(...) n object like http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android n also i think u shall consider timer class as singleton... – Braj Feb 18 '13 at 06:38

2 Answers2

2

In my opinion use Fragments instead of Activity because if you use fragments it is able to show two fragment on screen which contains timer and your remaining content.

If you use activity it is not possible to show accurate timer values while you move from once screen to other screen.

koti
  • 3,681
  • 5
  • 34
  • 58
0

How do you start the subsequent activities? I suppose with startActivity() or startActivivtForResult() , if this is the case you cold pass the time in the intent. But it is not a good solution because in the process you would "lose time" because the activity staring is not exact to the second. Why not create a timer activity that starts at the end of the process and finishes at the end?

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157