#Tip 1 – static newInstancemethod pattern Note: Avoid setter methods and parametrized constructors to modify fragment state, because platform uses only default (0 parameter) constructor, arguments bunde and saved state bundle to restore it later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static MyFragment newInstance(int someParam) { Bundle args = new Bundle(); args.putInt(ARGS_SOME_PARAM, someParam); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); int parameterValue getArguments().getInt(ARGS_SOME_PARAM); ... } |
#Tip 2 – single fragment in Activity If your activity role is only to show fragment don’t create custom layout …