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?