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;
}
}
}