How to call fragment method from main activity
Asked Answered
L

5

17

I have method in fragment class. I want to call that method from main activity but I don't want to use FragmentById (or) FragmentByTag.

My fragment method:

public void setItemFromDrawer(String sourceTag, String destTag) {
    //dosomething
}

How to call above method from main activity without using FragmentById (or) FragmentByTag?

Lipread answered 2/3, 2016 at 12:31 Comment(4)
when you load fragment using fragmentTransaction save fragment object and later on you can call any public method from that objectSeleta
I didn't understand.. Can you give any example codeLipread
possible duplicate of #10903577Causeway
try this https://mcmap.net/q/88078/-calling-a-fragment-method-from-a-parent-activityCenterpiece
S
41

First create an interface

public interface MyInterface
{
    void myAction() ;
}

Your fragment must implement this interface.

public MyFragment extends Fragment implements MyInterface

In your activity, define a field of type MyInterface :

  private MyInterface listener ;

  public void setListener(MyInterface listener)
  {
     this.listener = listener ;
  }

When creating your fragment and adding it :

setListener(myFragment);

Finally, when the condtion happens that you want to call the Fragment method, just call :

listener.myAction() ; // this will call the implementation in your MyFragment class.
Scurrilous answered 2/3, 2016 at 13:1 Comment(5)
I always find interfaces effective and the best option. This is the answer I'll recommend more.Trinitrophenol
Simple and best answer!Toughminded
setListener(myFragment); - where do you set that???????Rog
When creating your fragment and adding it! in your activity. @RogScurrilous
What if I add it in ViewPager?Rog
O
9

it means your calling a fragment method

((YourFragmentClass) fragment).Yourmethod();
Omeara answered 3/3, 2016 at 5:30 Comment(0)
R
8

To better explain the answer by user5466222 :

YourFragmentClass fragment = new YourFragmentClass();
((YourFragmentClass) fragment).yourmethod();
Rosiorosita answered 2/4, 2018 at 9:8 Comment(1)
If it was just that easy .... but it's not. Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object referenceRog
S
2

In Activity use something like this where you load your fragment:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(container, fragment);

transaction.addToBackStack(null); // if you want to store transaction        
transaction.commit();
currentFragment = fragment; // currentFragment is global Fragment variable

Use following line where you want to call fragment's method

currentFragment.setItemFromDrawer("sourceTag","destTag");
Seleta answered 2/3, 2016 at 12:42 Comment(2)
currentFragment = fragment; In this line fragment is what?Lipread
the fragment you are loading. in which your function implementedSeleta
O
-2

((YourFragment Class) fragment).Your method();

its worked form me

Omeara answered 2/3, 2016 at 13:12 Comment(1)
fragment meant what?Lipread

© 2022 - 2024 — McMap. All rights reserved.