Example #1
0
    private FrameLayout constructFragment(Context context)
        throws InstantiationException, IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
      TestAttributeSet attributeSet =
          new TestAttributeSet(
              attributes, resourceExtractor, attrResourceLoader, View.class, isSystem);
      if (strictI18n) {
        attributeSet.validateStrictI18n();
      }

      Class<? extends Fragment> clazz = loadFragmentClass(attributes.get("android:name"));
      Fragment fragment = ((Constructor<? extends Fragment>) clazz.getConstructor()).newInstance();
      if (!(context instanceof FragmentActivity)) {
        throw new RuntimeException(
            "Cannot inflate a fragment unless the activity is a FragmentActivity");
      }

      FragmentActivity activity = (FragmentActivity) context;

      String tag = attributeSet.getAttributeValue("android", "tag");
      int id = attributeSet.getAttributeResourceValue("android", "id", 0);
      // TODO: this should probably be changed to call TestFragmentManager.addFragment so that the
      // inflated fragments don't get started twice (once in the commit, and once in
      // ShadowFragmentActivity's
      // onStart()
      activity.getSupportFragmentManager().beginTransaction().add(id, fragment, tag).commit();

      View view = fragment.getView();

      FrameLayout container = new FrameLayout(context);
      container.setId(id);
      container.addView(view);
      return container;
    }