How to save time from the chronometer and access it from another activity

时间:2016-07-11 19:05:33

标签: android android-activity chronometer

The goal is to save the time shown on the Chronometer. I want to press stop and have the value stored in a variable so I can call it in another activity.

So far I can store the time on the Chronometer to a variable with the function showTime, the time I want to push to another activity is stored in elapsedSecs.

public void showTime() {
        Chronometer chronometerLeft = (Chronometer) findViewById(R.id.chronometerLeft);
        long elapsedMillis = SystemClock.elapsedRealtime() - chronometerLeft.getBase();
        double elapsedSecs = elapsedMillis / 1000;
    }

My issue is when I try to call showTime in the other activity I get the error that the showTime function needs to be static and when I make it static I can't use findViewById.

Goal: The end goal is to be able to press stop on my ChronometerActivity and then from my DataActivity be able to press a button to collect the data and the time to pop up.

i.e. Before data collect:

Time Elapsed:

After data collect:

Time Elapsed: 45 seconds

I can provide more code with regards to my project if necessary. Thanks!

1 个答案:

答案 0 :(得分:1)

You read it in the chronometer activity and send it to the second activity via Intent. You cannot share a view between activities or access it directly. Nor can you call a method of another activity directly- you don't have a copy of that activity to call it on.