I see in the Android Fragments Dev Guide that an "activity can call methods in a fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById()
or findFragmentByTag()
."
The example that follows shows how to get a fragment reference, but not how to call specific methods in the fragment.
Can anyone give an example of how to do this? I would like to call a specific method in a Fragment from the parent Activity. Thanks.
This question is related to
android
android-fragments
you also call fragment method using interface like
first you create interface
public interface InterfaceName {
void methodName();
}
after creating interface you implement interface in your fragment
MyFragment extends Fragment implements InterfaceName {
@overide
void methodName() {
}
}
and you create the reference of interface in your activity
class Activityname extends AppCompatActivity {
Button click;
MyFragment fragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
click = findViewById(R.id.button);
fragment = new MyFragment();
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment.methodName();
}
});
}
}
First you create method in your fragment
like
public void name()
{
}
in your activity
you add this
add onCreate()
method
myfragment fragment=new myfragment()
finally call the method where you want to call add this
fragment.method_name();
try this code
((HomesFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_container)).filterValidation();
From fragment to activty:
((YourActivityClassName)getActivity()).yourPublicMethod();
From activity to fragment:
FragmentManager fm = getSupportFragmentManager();
//if you added fragment via layout xml
YourFragmentClass fragment =
(YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
Too late for the question but will post my answer anyway for anyone still needs it. I found an easier way to implement this, without using fragment id or fragment tag, since that's what I was seeking for.
First, I declared my Fragment in my ParentActivity class:
MyFragment myFragment;
Initialized my viewPager as usual, with the fragment I already added in the class above. Then, created a public method called scrollToTop in myFragment that does what I want to do from ParentActivity, let's say scroll my recyclerview to the top.
public void scrollToTop(){
mMainRecyclerView.smoothScrollToPosition(0);
}
Now, in ParentActivity I called the method as below:
try{
myFragment.scrollToTop();
}catch (Exception e){
e.printStackTrace();
}
FragmentManager fm = getFragmentManager();
MainFragment frag = (MainFragment)fm.findFragmentById(R.id.main_fragment);
frag.<specific_function_name>();
If you are using “import android.app.Fragment;” Then use either:
1)
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.specific_function_name();
Where R.id.example_fragment is most likely the FrameLayout id inside your xml layout. OR
2)
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentByTag(“FragTagName”);
fragment.specific_function_name();
Where FragTagName is the name u specified when u did:
TabHost mTabHost.newTabSpec(“FragTagName”)
If you are using “import android.support.v4.app.Fragment;” Then use either:
1)
ExampleFragment fragment = (ExampleFragment) getSupportFragmentManager().findFragmentById(R.id.example_fragment);
fragment.specific_function_name();
OR
2)
ExampleFragment fragment = (ExampleFragment) getSupportFragmentManager().findFragmentByTag(“FragTagName”);
fragment.specific_function_name();
If you're using a support library, you'll want to do something like this:
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.my_fragment);
fragment.myMethod();
((FragmentName) getFragmentManager().findFragmentById(R.id.fragment_id)).methodName();
2. If you're using a support library Fragment, then do the following:
((FragmentName) getSupportFragmentManager().findFragmentById(R.id.fragment_id)).methodName();
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.
I think the best is to check if fragment is added before calling method in fragment. Do something like this to avoid null exception.
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
if(fragment.isAdded()){
fragment.<specific_function_name>();
}
Source: Stackoverflow.com