Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Created April 1, 2018 17:57
Show Gist options
  • Save ziginsider/00bc4f86fda1d427a2bd0ea7fd39c595 to your computer and use it in GitHub Desktop.
Save ziginsider/00bc4f86fda1d427a2bd0ea7fd39c595 to your computer and use it in GitHub Desktop.
public abstract class AbstractFragment extends Fragment {
private static final String KEY_COLOR = "color";
private int color;
@LayoutRes
private int layout;
@IdRes
private int idColorView;
public AbstractFragment(int layout, int idColorView) {
this.layout = layout;
this.idColorView = idColorView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
color = savedInstanceState.getInt(KEY_COLOR);
} else {
color = RandomColor.generateNewColor();
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(layout, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(idColorView).setBackgroundColor(color);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_COLOR, color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment