0

How can I save my chronometer value? So when I, for example, press the back button or when i "kill" the application, I don't want the timer to reset to 00:00.

The code is a a button that you hold and release. When I hold the button, a timer starts, and the timer ticks until I release the button. I want this time to never reset so I can see how much time I use in total when pressing the button. Anyone please help me! :)

This is my code:

public class MainActivity extends ActionBarActivity {

    Button button1;
     Chronometer chromo;
     protected long time;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1=(Button)findViewById(R.id.button1);
        chromo=(Chronometer)findViewById(R.id.chromo);
    }

    button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
            if(event.getAction() == MotionEvent.ACTION_DOWN){

                chromo.setBase(SystemClock.elapsedRealtime()+time);
                chromo.start();
            }else if( event.getAction() == MotionEvent.ACTION_UP){



                time =chromo.getBase()-SystemClock.elapsedRealtime();
                chromo.stop();
            }

            return true;
        }
    }
}
mmking
  • 1,564
  • 4
  • 26
  • 36
Leitvoll
  • 53
  • 4

2 Answers2

0

You should use sharedPreferences to save temporary data in the app. Search for it: there are dozens of tutorial about it.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
M-Garebaghi
  • 89
  • 1
  • 6
0

Your best best is probably going to be to use SharedPreferences. The main point of this is to make certain settings of your app persist past the current instance (which is roughly your problem).

// 0 - for private mode`
 SharedPreferences pref = getApplicationContext().getSharedPreferences("NameForMyPrefs", 0); 

 Editor editor = pref.edit();
 editor.putLong("time", time);

 editor.putString("otherRelevantData", "Kittens");
 editor.commit();

EDIT:

I changed the way to initialize it in code to be a little clearer.

EDIT2:

The Link posted by @Tr4X (which leads here ) has a pretty solid answer by @2Dee which you should really consider.

Happy Coding! Leave a comment if you have any questions.

Community
  • 1
  • 1
Matt
  • 5,404
  • 3
  • 27
  • 39
  • It's not useful anymore to initialize an Editor, you can directly call pref.edit().putLong(...).apply(); – Tr4X Jan 09 '15 at 15:33
  • Definitely true if he's only saving one value. – Matt Jan 09 '15 at 15:35
  • False, you can save as many value as you want with this, you just have to make multiple calls – Tr4X Jan 09 '15 at 15:36
  • I just cant make things work! Can you be a bro and try to code this for me? Im very new with this.. And i've tried a lot! You have my code, and you know exactly what my goal is. I'd appreciate an answer. – Leitvoll Jan 09 '15 at 15:44
  • @Tr4X, yeah, but the point of saving the reference is so that you don't have to invoke the method every time. The overhead is very little as you're only creating a shallow copy, not a deep copy. – Matt Jan 09 '15 at 15:58
  • @Traczy, I think literally pasting this code, the code posted by Tr4x, or 2Dee (in the other post) will take care of it. I think it actually is copy/paste. If something's still not working, perhaps you can further describe the behavior in one of your questions. – Matt Jan 09 '15 at 16:00
  • Ok so lets look at 2Dee's code. I dont understand what i should paste where. Could you help me with that? :) @Matt – Leitvoll Jan 09 '15 at 16:05
  • I think so, let's move over to that question – Matt Jan 09 '15 at 16:06