Created
April 1, 2018 17:57
-
-
Save ziginsider/00bc4f86fda1d427a2bd0ea7fd39c595 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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