Too late for the question but this is a easy way to get fragment instance and call methods in a fragment; you have to get instance of your fragment then call your public method:
In your fragment :
private static yourFragment instance;
then in onCreateView
of your fragment :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
instance= this;
View v = inflater.inflate(R.layout.fragment_tools, container, false);
binding = FragmentToolsBinding.inflate(inflater, container, false);
return v;
}
and also in your fragment you have to have a static method that returns the instance:
public static yourFragment GetInstance()
{
return instance;
}
then you have a public method in in your fragment that you want to call it like this:
public void theMethod()
{
Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
}
then you can get fragment instance and call your non static public method like this:
yourFragment frag = yourFragment.GetInstance();
frag.theMethod();