How to Know Which Fragment the User Is Viewing

How to know if a Fragment is Visible?

You should be able to do the following:

MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
//DO STUFF
}
else {
//Whatever
}

How to know if any fragment is active or visible?

If you want to change title of toolbar in activity for different fragments than use interface.

Create interface :

public interface OnFragmentTitleChangeListener {
void onFragmentTitle(String strTitle);
}

Implement this interface in your activity

public class YourActivity extends AppCompatActivity implements OnFragmentTitleChangeListener
{
TextView title;
......
......
// Initialize title in onCreate method
......
......
// Override this method
public void onFragmentTitle(String strTitle)
{
title.setText(strTitle);
}

@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();

if (onBack(fm)) {
return;
}
super.onBackPressed();
}

private boolean onBack(FragmentManager fm) {
if (fm != null) {
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
return true;
}
}
return false;
}
}

After that set text from fragment's onResume method.

@Override
public void onResume() {
super.onResume();
((OnFragmentTitleChangeListener) mContext).onFragmentTitle(getResources().getString(R.string.yourtext));
}

How to check if the fragment is visible to user

I finally came across the solution while trying myself:

Simple check for isResumed() and it'd only return true if the fragment is in the foreground and resumed state.

Just to be safe, check for isVisible which also checks for isAdded internally and would reduce the condition, so a simple if-condition:

if (isVisible() && isResumed()) {
// perform your action here
}

This worked for me!

How to determine when Fragment becomes visible in ViewPager

How to determine when Fragment becomes visible in ViewPager

You can do the following by overriding setUserVisibleHint in your Fragment:

public class MyFragment extends Fragment {
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
else {
}
}
}

How to test if a fragment view is visible to the user?

You're right there is a better way to do this!

Have a look at the FragmentPagerAdapter javadoc online and you'll see there is a method setPrimaryItem(ViewGroup container, int position, Object object):void doing exactly what you need.

From the javadoc

public void setPrimaryItem (ViewGroup container, int position, Object object)

Called to inform the adapter of which item is currently considered to
be the "primary", that is the one show to the user as the current
page.

Parameters container The containing View from which the page will be
removed. position The page position that is now the primary.
object The same object that was returned by instantiateItem(View,
int).

Note on scroll state

Now if you implement this and start debugging to get a feel of when exactly this is called you'll quickly notice this is triggered several times on preparing the fragment and while the user is swiping along.

So it might be a good idea to also attach a ViewPager.OnPageChangeListener and only do what has to be done once the viewpagers scroll state becomes SCOLL_STATE_IDLE again.



Related Topics



Leave a reply



Submit