Issue
I have extended the MaterialButton to display a consistent loading state on the button when clicked however I ran into a minor issue when implementing SavedState restoration.
The progress button variant of this I have written, disables the view, displays an animated loading spinner, optionally displays loading text and restores then restores the previously displayed text/state when the view is enabled.
MaterialButton.SavedState is package-private so cannot be extended externally. This does not create a problem with my implementation as I don't use the 'checked' field it extends but it does raise these questions:
TLDR
- Is it wrong to extend this MaterialButton?
- If not would it be appropriate to submit a PR to the public repo to make SavedState public?
material-components-android:1.2.0
Solution
Is it wrong to extend this MaterialButton?
The MaterialButton
is not a final class then you can extend it, and for example the ExtendedFloatingActionButton
extends it.
If not would it be appropriate to submit a PR to the public repo to make SavedState public?
You don't need to make MaterialButton.SavedState
public. You can do something like:
public class MyButton extends MaterialButton
{
private String text;
//....
static class SavedState extends AbsSavedState {
@Nullable CharSequence myText;
SavedState(Parcelable superState) {
super(superState);
}
SavedState(@NonNull Parcel source, ClassLoader loader) {
super(source, loader);
myText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
TextUtils.writeToParcel(myText, dest, flags);
}
@NonNull
@Override
public String toString() {
return "MyButton.SavedState{"
+ " text="
+ myText
+ "}";
}
public static final Creator<SavedState> CREATOR =
new ClassLoaderCreator<SavedState>() {
@NonNull
@Override
public SavedState createFromParcel(@NonNull Parcel in, ClassLoader loader) {
return new SavedState(in, loader);
}
@Nullable
@Override
public SavedState createFromParcel(@NonNull Parcel in) {
return new SavedState(in, null);
}
@NonNull
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
@Nullable
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.myText = text;
return ss;
}
@Override
public void onRestoreInstanceState(@Nullable Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
text = ss.myText.toString();
}
}
In this way the MyButton.SavedState
will contain also the MaterialButton.SaveState
without extending it.
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.