Reputation: 4834
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.
Upvotes: 3
Views: 7962
Reputation: 4644
HomeFragment.java
public class HomeFragment extends Fragment {
private static HomeFragment instance;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
instance = this;
return view;
}
public static HomeFragment getInstance() {
return instance;
}
public void myMethod() {
// do something...
}
}
AnotherClass.java
public Class AnotherClass() {
// call this method
HomeFragment.getInstance().myMethod();
}
Upvotes: 7
Reputation: 1563
Create interface for that
public interface Callback{
void refreshList();
}
This is your fragment
public class CustomFrag extends Fragment implements Callback{
...
new SomeClass().setListener(CustomFrag .this);
@Override
public void refreshList(){
//do work
}
}
Write your class
public class SomeClass{
private Callback mCallback;
public void setListener(Callback callBack){
mCallback=callBack;
}
}
call Fragment refreshListview from your SomeClass like this
mCallback.refreshListView();
Upvotes: 0
Reputation: 2783
The issue is that you need to get the activity context of the fragments. You cannot just make up a new class extending a fragment class, without actually instantiating the fragment and associating it with an activity, and hope to get back your activity context from it.
If you wish to use SomeClass
independent of the Fragment, you can declare a variable to hold the parent activity
private FragmentActivity mActivity;
and then in the constructor, you may pass the activity context reference into it.
public SomeClass (FragmentActivity activity) {
mActivity = activity;
...
}
Afterwards, you can call
mActivity.getSupportFragmentManager().findFragmentByTag(tagName);
when you need it within SomeClass
.
Upvotes: 1