1

I have this Chronometer in my main activity and I want to get the value of this chronometer after the user finished the given task in the mainActivity.class and display it to the next activity (end.class)

MainActivity.class snippet:

private void showElapsedTime() {

        long timeElapsed = SystemClock.elapsedRealtime() - chrono.getBase();      
            int millis = (int) timeElapsed;
            int seconds = (int) timeElapsed/1000;
            int minutes = seconds/60;

            Toast.makeText(this, "Level1 - Elapsed time: " + minutes + ":" + seconds, 
                    Toast.LENGTH_LONG).show();

        }

end.class:

TextView Set1;      

Set1 = (TextView) findViewById (R.id.time1);

How can I get the chronometer value from the MainActivity.class and display it in my textview in end.class?

pingboo23
  • 137
  • 1
  • 3
  • 15
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Simon Aug 21 '13 at 09:46
  • Are you get the chronometer time in mainactivity class or not. – Aravin Aug 21 '13 at 09:47
  • Who initiates the start of `End`? Do you create the intent in `MainActivity` or do you use some other way? – Vincent van der Weele Aug 21 '13 at 09:48
  • I know how to pass data between activities, I just don't know how to get the value of the chronometer. I tried Intent and bundle but it doesn't work. – pingboo23 Aug 21 '13 at 09:48
  • @Heuster I created intent in MainActivity to move to second activity after the user finishes the task given. – pingboo23 Aug 21 '13 at 09:50
  • Chronometer is already declared before the onCreate method. – pingboo23 Aug 21 '13 at 09:50
  • @pingboo23 So, if I understand correctly, you already have an intent `i` for `End` and when the user finishes the task, you call `startActivity(i)`? Then, just before that line, add `long timeElapsed = SystemClock.elapsedRealtime() - chrono.getBase();` and `i.putExtra("timeElapsed", timeElapsed)`. – Vincent van der Weele Aug 21 '13 at 10:00
  • @Heuster then I'll just set it to the end.class? extract intent and display the value? – pingboo23 Aug 21 '13 at 10:03
  • @pingboo23 yes, there you call `getIntent().getExtras().getLong("timeElapsed")` and use it however you like (or simply `getIntent().getLongExtra("timeElapsed", 0L)`) – Vincent van der Weele Aug 21 '13 at 10:06
  • @Heuster How can I display `timeElapsed` ? I can't used it. – pingboo23 Aug 21 '13 at 10:12
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35904/discussion-between-heuster-and-pingboo23) – Vincent van der Weele Aug 21 '13 at 10:13
  • `I tried Intent and bundle but it doesn't work` Of course it works, thousands of developers use it every day. Your code is the problem but you haven't shown it. – Simon Aug 21 '13 at 10:14
  • @Simon I've already shown it in my other post, but didn't resolved it. I create this new post to get new answers. – pingboo23 Aug 21 '13 at 10:29
  • I've read both of these posts, and my best advice is to learn how to use the debugger. It's really easy and if you knew how to use it, you would find this problem in minutes. NullPointerExceptions are very common and also very easy to solve. – Simon Aug 21 '13 at 12:39
  • @Simon already solved my problem by the help of Heuster :) – pingboo23 Aug 21 '13 at 13:02

1 Answers1

0

The setBase() is used to set the time that the count-up timer is in reference to. . You need to hold on to this value somewhere outside the activity, like in the Application class. Whenever your activity has resumes, you setBase() to this reference time.

Shyam
  • 6,376
  • 1
  • 24
  • 38