I have a Fragment:
public class CustomFrag extends Fragment{
...
public void refreshList(){
...
}
}
I have a separate Class:
public class SomeClass{
...
}
I am trying to call refreshList()
from SomeClass
:
String tagName = "android:switcher:" + R.id.pager + ":" + 1;
CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
f2.refreshList();
But this shows Cannot resolve method 'getActivity'
. If I add to the class:
extends Fragment
All the warnings go away, but the app crashes, with a null-pointer exception to the CustomFrag f2 = (CustomFrag)...
line.
I have another fragment, contained within the same parent as CustomFrag
, and the method call described above works great.
How can I access the CustomFrag
methods from SomeClass
? This question (and similar) are asked all over, but most do not have accepted answers, or else have very vague ones that are of little help.
Thanks in advance.
Edit: Temp solution
I am calling methods that belong to SomeClass
from within the two aforementioned Fragments. What I came up with is the following:
Within FragOne
public class FragOne extends Fragment{
...
String tagName = "android:switcher:" + R.id.pager + ":" + 1;
CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
}
And then:
public class FragOne extends Fragment{
...
String tagName = "android:switcher:" + R.id.pager + ":" + 1;
CustomFrag f2 = (CustomFrag)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
SomeClass obj = new SomeClass(...);
obj.someMethod(f2);
}
Where someMethod
then can use
f2.refreshList();
This solves the issue I have been having, but it would still be nice to know a more direct way to access the Fragment's methods via a separate Class.
Further answers that solve this problem are welcome, and will be accepted as solution.