0

I'm using an Array to store my fragments like this:

fragmentList = new Fragment[]{
            new FragmentA(),
            new FragmentB(),
            new FragmentC(),
            ...        
};

Later they will be inserted in a scrollview. The amount of fragmets doesn't change but the sequence can change. What I want is to call a public method always from the first Fragment in the Array.

I tried something like this, but there is a syntax error.

 ((fragmentList[0])fragmentManager.findFragmentById(R.id.fragmentA)).publicMethod();

How can I call the public Method from the first Fragment?

EDIT:

I tried to set a Tag to the new Fragment and then call the interface method. This is how I tried to replace and set the Tag.

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    for (int arrayIndex=0, fragmentNumber=1; fragmentNumber <= numberOfFragments; arrayIndex++, fragmentNumber++){
        fragmentTransaction.replace(r.getIdentifier("fragment" + fragmentNumber, "id", packageName), fragmentList[arrayIndex]);
        fragmentTransaction.add(fragmentList[arrayIndex],"fragment"+fragmentNumber);
    }
    ((Unfold)fragmentManager.findFragmentByTag("fragment1")).unfold();
    fragmentTransaction.commit();

But at the line ((Unfold)fragmentManager.findFragmentByTag("fragment1")).unfold();

I get the error: Attempt to invoke interface method 'void MyApp.Unfold.unfold()' on a null object reference

I think the Tag is not set correct?

WeSt
  • 889
  • 5
  • 14
  • 32

3 Answers3

1

You need to cast the fragment. For example: ((FragmentA)fragmentManager.findFragmentById(R.id.fragmentA)).publicMethod();

In case all the fragments suppose to implement this method. I'll recommend to create an interface with that function and to implement in all the fragments. Then, cast using the interface for example:

public interface MyInterface{...}
((MyInterface)fragmentManager.findFragmentById(R.id.fragmentA)).publicMethod();
fbwnd
  • 687
  • 6
  • 12
  • I get this exception: java.lang.NullPointerException: Attempt to invoke interface method 'void InterfaceA.pubMethodA()' on a null object reference – WeSt Jul 24 '17 at 13:01
  • I assume that fragmentManager.findFragmentById(R.id.fragmentA)) return null. You can see https://stackoverflow.com/questions/8532462/how-to-get-the-fragment-instance-from-the-fragmentactivity/ maybe it will help – fbwnd Jul 24 '17 at 13:03
0

You have to cast to a known class. Change your syntax to this. This requires that your Fragment are all of YourFragmentClass. Otherwise you have to implement an Interface in every Fragment of yours with the public method.

((YourFragmentClass)fragmentManager.findFragmentById(R.id.fragmentA)).publicMethod();

The implementation of your Interface could look like this

public interface YourCommonInterface {
    public void commonMethod();
}

Your Fragments would implement that interface and you would cast it to that e.g.

((YourCommonInterface)fragmentManager.findFragmentById(R.id.fragmentA)).publicMethod();

This has the advantage that you can have different kind of Fragments.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • @fbwnd My answer was literally 20 seconds after yours. Sometimes it takes time to write an answer while someone already submitted one. – Murat Karagöz Jul 24 '17 at 12:55
0

Use events and handler for this. Register an event handler in your fragment whose public method you want to call.

S.D
  • 85
  • 8