Jamsheed Kamarudeen
Jamsheed Kamarudeen

Reputation: 728

Calling a function in a currently displayed fragment from Application

I hav a fragment in my MainActivity which displays a loading pic as of now, while I call a function in my XMLReader Application class from the MainActivity itself. The XMLReader uses an asynctask to read an xml file from an online server. after execution, i need to alert the fragment that currently shows the loading pic to refresh to something else. My question is how do i alert the currently displayed fragment? or atleast alert the Activity so that i can call a different fragment.

read somewhere that putting a broadcast listener would be overkill, is it my only way?

Upvotes: 0

Views: 83

Answers (2)

sonida
sonida

Reputation: 4411

If you want to access the current visible item in viewpager:

// from activity
int position = mViewPager.getCurrentItem();
YourFragment yourFragment = (YourFragment) mSectionsPagerAdapter.getItem(position);
yourFragment.refreshSomething();

Upvotes: 0

Neoh
Neoh

Reputation: 16174

If you want to access the current visible item in viewpager:

// from activity
int position = mViewPager.getCurrentItem();
Fragment currentItem = mSectionsPagerAdapter.getItem(position);

If you want to callback the activity from fragment:

// call custom method update() in activity from fragment
((MainActivity) getActivity()).update();

Upvotes: 1

Related Questions