Tuesday, 15 June 2010

android - Get fragment object in activity while using viewPagerviewpager -



android - Get fragment object in activity while using viewPagerviewpager -

recently, trying know how swipe through fragments using viewpager. have set text on textview in fragement activity. found 1 can fragment object viewpager using :

getsupportfragmentmanager().findfragmentbytag("android:switcher:" + r.id.pager + ":0");

when tried in oncreate() method of activity, returns null. assume because fragment not started yet. in method should seek access fragment object can send arguments fragment methods?

if have viewpager , want access fragment within viewpager, can utilize code:

fragment currentfragment = (fragment) viewpager.getadapter().instantiateitem(viewpager, viewpager.getcurrentitem()); //gets current fragment //now have cast fragment, let's it's name senapatifragment ((senapatifragment)currentfragment).somemethod(...); // have access public fields , methods within fragment

android android-fragments android-viewpager fragment

No comments:

Post a Comment