Reputation: 4120
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.
Upvotes: 139
Views: 221068
Reputation: 11878
Set a tag when you showing your fragment:
MyFragment fragment = new MyFragment();
fragment.show(getSupportFragmentManager(), "<TAG>");
Later, to get the fragment instance, you can find by tag:
MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentByTag("<TAG>");
fragment.<YOU_METHOD>;
Upvotes: 0
Reputation: 484
This works in my case hope anyone get help from this
OnDemandFrag frag = new OnDemandFrag();
using this to replace fragment in main
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_frame, frag).commit();
i just created a global instance of fragment in the MainActivity and then from this instance run function of Fragment
frag.hidelayer();
Upvotes: 0
Reputation: 311
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();
Upvotes: 8
Reputation: 15701
not get the question exactly as it is too simple :
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>();
Upvotes: 231
Reputation: 224
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();
}
Upvotes: 0
Reputation: 123
((HomesFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_container)).filterValidation();
Upvotes: 0
Reputation:
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();
}
});
}
}
Upvotes: 4
Reputation: 11601
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.
Upvotes: 1
Reputation: 2454
FragmentManager fm = getFragmentManager();
MainFragment frag = (MainFragment)fm.findFragmentById(R.id.main_fragment);
frag.<specific_function_name>();
Upvotes: 1
Reputation:
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
Upvotes: 1
Reputation: 361
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");
Upvotes: 5
Reputation: 11267
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();
Upvotes: 82
Reputation: 1764
((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();
Upvotes: 6
Reputation: 269
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>();
}
Upvotes: 3
Reputation: 4328
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();
Upvotes: 10