0

In my Android app, a fragment uses a library I wrote. What I need to do is use reflection to have a method within the library access a method in the fragment. My code looks like this:

Context c = getContext();

Class[] paramTypes = new Class[2];
paramTypes[0] = int.class;
paramTypes[1] = boolean.class;

java.lang.reflect.Method method = c.getClass().getMethod("someMethod", paramTypes);

method.invoke(c, id, selected);

If the method "someMethod" is placed in the fragment class, it never gets called. But if I place it in the activity that the fragment is part of, it will get called. It appears that the getContext() method is referring to the activity and not the fragment class.

How can I get it to refer to the fragment class? However, keep in mind that my library could be used anywhere including the activity itself. So what I don't want is to reference the context of the fragment but rather the context of whatever class happens to be the one that is using the library.

Johann
  • 27,536
  • 39
  • 165
  • 279

1 Answers1

0

Apparently you cannot create click events in xml that call the click method in a fragment.

See: Android app crashing (fragment and xml onclick)

Community
  • 1
  • 1
Johann
  • 27,536
  • 39
  • 165
  • 279