Search code examples
androidchronometer

Android. Save and reestablish value of chronometer (reversing screen)


My question is rather simple, but i can't handle with him. I have chronometer field in my design.

I want to save them for avoid lost value of time by revert the screen (recreate new activity)

I use for save

    public Object onRetainConfigurationInstance(){
    return timeSeconds;
}

And for reestablish timeSeconds = (Chronometer) getLastNonConfigurationInstance();

But it doesn't work. Could somebody help me? What parameter i must save for reestablish chronometer time?


Solution

  • protected void onSaveInstanceState(Bundle outState) {
        outState.putLong(TIME_KEY, timeSeconds.getBase());
        super.onSaveInstanceState(outState);
    }
    
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        if((savedInstanceState !=null)
                && savedInstanceState.containsKey(TIME_KEY)){
            timeSeconds.setBase(savedInstanceState.getLong(TIME_KEY));
        }
        super.onRestoreInstanceState(savedInstanceState);
    }