Skip to content

Commit

Permalink
Added support to save internal state of TypedTextView across lifecycl…
Browse files Browse the repository at this point in the history
…e owner's state changes.
  • Loading branch information
iamporus committed Jun 18, 2019
1 parent 270c777 commit 1b49f4e
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.content.res.TypedArray;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.RawRes;
import android.support.annotation.StringRes;
Expand Down Expand Up @@ -469,4 +471,72 @@ private void onViewStopped()
mMediaPlayer.pause();
}
}

@Override
public Parcelable onSaveInstanceState()
{
Parcelable superState = super.onSaveInstanceState();
SavedState savedState = new SavedState( superState );
savedState.setCurrentIndex( mIndex );
return savedState;
}

@Override
public void onRestoreInstanceState( Parcelable state )
{
SavedState savedState = ( SavedState ) state;
super.onRestoreInstanceState( savedState.getSuperState() );
mIndex = savedState.getCurrentIndex();
}

/**
* Class to save view's internal state across lifecycle owner's state changes.
*/
private static class SavedState extends BaseSavedState
{
private int mCurrentIndex;

SavedState( Parcel source )
{
super( source );
mCurrentIndex = source.readInt();
}

SavedState( Parcelable superState )
{
super( superState );
}

void setCurrentIndex( int currentIndex )
{
mCurrentIndex = currentIndex;
}

int getCurrentIndex()
{
return mCurrentIndex;
}

@Override
public void writeToParcel( Parcel out, int flags )
{
super.writeToParcel( out, flags );
out.writeInt( mCurrentIndex );
}

public static final Parcelable.Creator< SavedState > CREATOR = new Creator< SavedState >()
{
@Override
public SavedState createFromParcel( Parcel parcel )
{
return new SavedState( parcel );
}

@Override
public SavedState[] newArray( int size )
{
return new SavedState[ size ];
}
};
}
}

0 comments on commit 1b49f4e

Please sign in to comment.