Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
onSaveInstanceState method is overridden to save position.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceryle committed Nov 17, 2016
1 parent 8d2956d commit 1ed49dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
Expand Down Expand Up @@ -347,6 +349,8 @@ private void toggle(int position, int duration) {

if (null != onClickedButtonPosition)
onClickedButtonPosition.onClickedButtonPosition(position);

this.position = position;
}

private boolean hasWidth = false;
Expand Down Expand Up @@ -791,4 +795,23 @@ public Interpolator getInterpolatorSelector() {
public int getMargin() {
return margin;
}

@Override
public Parcelable onSaveInstanceState() {
// Log.d(TAG, "onRestoreInstanceState: " + position);
Bundle bundle = new Bundle();
bundle.putParcelable("state", super.onSaveInstanceState());
bundle.putInt("position", position);
return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
position = bundle.getInt("position");
state = bundle.getParcelable("state");
}
super.onRestoreInstanceState(state);
}
}
24 changes: 19 additions & 5 deletions sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@

public class MainActivity extends AppCompatActivity {

private int position = 1;
private Button button;
private SegmentedButtonGroup group;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final SegmentedButtonGroup group = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
group.setPosition(position, false);
group = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
button = (Button) findViewById(R.id.button);

updateButton(group.getPosition());

group.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() {
@Override
public void onClickedButtonPosition(int position) {
updateButton(position);
}
});

final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = group.getPosition();
position = ++position % 3;
button.setText("Position: " + position);
updateButton(position);
group.setPosition(position, true);
}
});
}

private void updateButton(int position) {
button.setText("Position: " + position);
}
}

0 comments on commit 1ed49dd

Please sign in to comment.