Reputation: 123
I saved the fragment object using Streets Of Boston's answer here
And pulled out a fragment
Fragment fragment = myPagerAdapter.getRegisteredFragments(nowPosition);
if (fragment == null)
{
return;
}
but i'm can not access the public methods from fragment object fragment .
this method is in a fragment
public void dowork()
{
//WORK!
}
public static void dowork2()
{
//WORK!
}
fragment.dowork(); -> dowork is not found
fragment.dowork2(); -> dowork2 is not found
How can access?
thanks.
Upvotes: 3
Views: 1162
Reputation: 981
You have to cast that Fragment to your Subclass of Fragment. The reason you cannot access those methods is because they are not available in fragment.They are in your implementation of the Fragment class.
Upvotes: 3
Reputation: 2386
If you are trying to call these methods on the fragment as instantiated in your example, you'll need to cast the fragment to your class. So if your fragment is of class MyFragment
you need to do
MyFragment fragment = (MyFragment) myPagerAdapter.getRegisteredFragments(nowPosition);
Then you will be able to call the methods you declared in your fragment class.
Also, static methods should be called on the class, not an instance, so it would be correct to do
MyFragment.dowork2();
if that method actually is static.
Upvotes: 5