Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPDATE</strong>: Android Support Library (rev 11) finally <a href="https://github.com/android/platform_frameworks_support/commit/bf7974e46b008ffa4a16c61386735c4817d343aa" rel="noreferrer">fixed the user visible hint issue</a>, now if you use support library for fragments, then you can safely use <code>getUserVisibleHint()</code> or override <code>setUserVisibleHint()</code> to capture the changes as described by gorn's answer.</p> <p><strong>UPDATE 1</strong> Here is one small problem with <code>getUserVisibleHint()</code>. This value is by default <code>true</code>.</p> <pre><code>// Hint provided by the app that this fragment is currently visible to the user. boolean mUserVisibleHint = true; </code></pre> <p>So there might be a problem when you try to use it before <code>setUserVisibleHint()</code> was invoked. As a workaround you might set value in <code>onCreate</code> method like this.</p> <pre><code>public void onCreate(@Nullable Bundle savedInstanceState) { setUserVisibleHint(false); </code></pre> <hr> <p>The outdated answer:</p> <p>In most use cases, <code>ViewPager</code> only show one page at a time, but the pre-cached fragments are also put to "visible" state (actually invisible) if you are using <code>FragmentStatePagerAdapter</code> in <code>Android Support Library pre-r11</code>.</p> <p>I override :</p> <pre><code>public class MyFragment extends Fragment { @Override public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { // ... } } // ... } </code></pre> <p>To capture the focus state of fragment, which I think is the most suitable state of the "visibility" you mean, since only one fragment in ViewPager can actually place its menu items together with parent activity's items.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload