Skip to content

Commit

Permalink
save instance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Kwiecień committed Jun 9, 2015
1 parent b019b49 commit 71439de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 22
versionCode 8
versionName "1.0.8"
versionCode 9
versionName "1.0.9"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
Expand All @@ -15,6 +16,7 @@ public class CountingTextView extends TextView {

private final String STATE = "state";
private final String PREVIOUS_VALUE = "previous_value";
private final String FORMATTER = "formatter";

private float previousValue;
private ValueFormatter formatter;
Expand Down Expand Up @@ -61,8 +63,18 @@ private void setValueAnimated(float value) {
setText(formatter.formatValue(value));
}

public static abstract class ValueFormatter {
public static abstract class ValueFormatter implements Parcelable {
public abstract String formatValue(float value);

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

}
}

public void setFormatter(ValueFormatter formatter) {
Expand All @@ -77,6 +89,7 @@ public void setAnimationDuration(long animationDuration) {
public Parcelable onSaveInstanceState() {
Bundle outState = new Bundle();
outState.putFloat(PREVIOUS_VALUE, previousValue);
outState.putParcelable(FORMATTER, formatter);
outState.putParcelable(STATE, super.onSaveInstanceState());
return outState;
}
Expand All @@ -85,7 +98,10 @@ public Parcelable onSaveInstanceState() {
public void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
previousValue = ((Bundle) state).getFloat(PREVIOUS_VALUE);
formatter = ((Bundle) state).getParcelable(FORMATTER);
super.onRestoreInstanceState(bundle.getParcelable(STATE));

if (previousValue != 0f) setValue(previousValue);
}

}

0 comments on commit 71439de

Please sign in to comment.