public void addFragment(int containerViewId, String tag, Fragment fragment, boolean replace) { fragmentsById.put(containerViewId, fragment); fragmentsByTag.put(tag, fragment); ShadowFragment shadowFragment = shadowOf(fragment); shadowFragment.setTag(tag); shadowFragment.setContainerViewId(containerViewId); shadowFragment.setShouldReplace(replace); shadowFragment.setActivity(activity); fragment.onAttach(activity); fragment.onCreate(shadowFragment.getSavedInstanceState()); }
@Implementation public String getString(int id) { if (activity == null) { throw new IllegalStateException("Fragment " + this + " not attached to Activity"); } return getResources().getString(id); }
public void startFragment(Fragment fragment) { ViewGroup container = null; ShadowFragment shadowFragment = shadowOf(fragment); if (shadowOf(activity).getContentView() != null) { container = (ViewGroup) activity.findViewById(shadowFragment.getContainerViewId()); } View view = fragment.onCreateView(activity.getLayoutInflater(), container, shadowFragment.getSavedInstanceState()); shadowFragment.setView(view); fragment.onViewCreated(view, null); if (container != null) { if (shadowFragment.getShouldReplace()) { container.removeAllViews(); } if (view != null) { container.addView(view); } } fragment.onActivityCreated(shadowFragment.getSavedInstanceState()); fragment.onStart(); }
public void callFragmentOnActivityCreated() { assertFragmentManager(); for (Fragment each : mfragmentManager.getFragments() .values()) { ShadowFragment shadow = Robolectric.shadowOf(each); Bundle savedInstanceState = shadow.getSavedInstanceState(); View view = each.onCreateView(each.getActivity() .getLayoutInflater(), null, savedInstanceState); shadow.setView(view); each.onViewCreated(view, null); each.onActivityCreated(savedInstanceState); } }
@Implementation public void onCreate(Bundle bundle) { super.onCreate(bundle); if (bundle != null && bundle.containsKey(FRAGMENTS_TAG)) { Object[] fragments = (Object[]) bundle.getSerializable(FRAGMENTS_TAG); for (Object o : fragments) { SerializedFragmentState fragmentState = (SerializedFragmentState) o; try { Fragment fragment = fragmentState.fragmentClass.newInstance(); shadowOf(fragment).setSavedInstanceState(bundle); fragmentManager.addFragment(fragmentState.containerId, fragmentState.tag, fragment, true); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } }
public void callFragmentOnCreate() { assertFragmentManager(); for (Fragment each : mfragmentManager.getFragments() .values()) { ShadowFragment shadow = Robolectric.shadowOf(each); Bundle savedInstanceState = shadow.getSavedInstanceState(); each.onCreate(savedInstanceState); } }
ShadowFragment shadow = Robolectric.shadowOf(newFragment); newFragment.setArguments(arguments); shadow.setSavedInstanceState(savedInstanceState);
fragmentsByTag.put(tag, fragment); shadowOf(fragment).setTag(tag); shadowOf(fragment).setContainerViewId(containerViewId); shadowOf(fragment).setShouldReplace(replace); shadowOf(fragment).setActivity(mActivity); fragment.onAttach(mActivity);