Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a method of a Fragment from the ViewPager Activity

I have a Fragment with a method setName() that changes an EditText text, by means of the setText function.

What is the best way to call that method from the activity that hosts that fragment by means of a ViewPager?

In other words, how can I access a Fragment's methods (which change that fragment's layout, for example) from the Activity that hosts that fragment by means of a ViewPager?

I am asking this because I have tried several ways, but always with errors.

like image 653
JZweige Avatar asked Jul 16 '13 19:07

JZweige


1 Answers

Best way to do this, just call

CallingFragmentName fragment = (CallingFragmentName) viewPager
                    .getAdapter()
                    .instantiateItem(viewPager, viewPager.getCurrentItem());

It will re-instantiate your calling Fragment, so that it will not throw null pointer exception.

like image 135
Ritesh Adulkar Avatar answered Oct 19 '22 18:10

Ritesh Adulkar