0

With update of Support library v4 to 23.0.0 version from 22.2.0, mActivity field has gone in Fragment.java class. From what I see, it seems to be replaced by mHost that has type FragmentHostCallback(abstract, the implementation is HostCallbacks, that is nested in FragmentActivity)

I'm heavily testing all my fragments with unit tests. Before, I created new Activity in the constructor of the test class (which extends AbstractApplicationTest), did some tweaks (like attaching context, etc.). And then via reflection set this new Activity as an activity for the fragment I want to test:

protected void setFragmentActivity(final Fragment fragment) throws NoSuchFieldException, IllegalAccessException {
    final Field field = Fragment.class.getDeclaredField("mActivity");
    field.setAccessible(true);
    field.set(fragment, activity);
}

Now, the mActivity is no longer exist.

Why is it important: when my fragment, which I'm testing, calls getActivity() without this setting, I'm getting null, as fragment really has no activity.

Any ideas how can it be workarounded? Have a brilliant day, Konstantin

Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95

1 Answers1

0

I figured out. In short, it's wrong to go with reflection and I stick to use ActivityUnitTestCase instead.

Please find full write-up about it here: Unit Test an Android Fragment

Community
  • 1
  • 1
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95