Bookmark this question. Show activity on this post. 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() ."
Simply call your parent activity using getActivity() method.
You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
not get the question exactly as it is too simple :
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<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();
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");
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>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With