I don't know about Java
, but in C#
(Xamarin.Android) there is no need to look up the fragment everytime you need to call the method, see below:
public class BrandActivity : Activity
{
MyFragment myFragment;
protected override void OnCreate(Bundle bundle)
{
// ...
myFragment = new MyFragment();
// ...
}
void someMethod()
{
myFragment.MyPublicMethod();
}
}
public class MyFragment : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle bundle)
{
// ...
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
// ...
}
public void MyPublicMethod()
{
// ...
}
}
I think in Java
you can do the same.