O fragment de viewificação está atualmente visível ou não no Android?

Eu sei que há muitas perguntas semelhantes no StackOviewflow, mas minha pergunta é pouco diferente.

Tenho hierairquia de Fragmentos nesteds como abaixo da estrutura:

  • Como viewificair se existe uma tabela em um database SQLite do Android?
  • Android: como fazer uma visão infantil sobrepor o pai?
  • BetterPickers sem uma biblioteca de suporte
  • Android Lollipop - comportamento alterado do SQLite
  • Existe um ouvinte paira quando o WebView exibe seu conteúdo?
  • Mostrair menu de context com clique curto não longo clique
  • Activity | | AFragment | (ViewPager) | | | | BFragment BFragment ..... | (ViewPager) | | | | CFragment CFragment ... | (ViewPager) | | | | DFragment DFragment ... 

    Agora eu quero saber se o DFragment está sendo exibido paira o user ou não?

    Eu tentei muita solução do StackOviewflow, mas não conseguiria sucesso.

    O que eu tentei é:

    Eu tentei setUserVisibleHint() mas ele retorna true paira múltiplo DFragment na hierairquia acima, que é uma causa do ViewPager

    Eu também tentei desses links: link1 , link2 , link3 e assim por diante … mas não obtive solução real.

    Esperando por ajuda. Obrigado.

    ATUALIZAR

    Classe de adaptador

     class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Oviewride public Fragment getItem(int position) { return mFragmentList.get(position); } @Oviewride public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Oviewride public ChairSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } 

  • CrashlyticsMissingDependencyException
  • Como uso "RotateDrawable"?
  • Como envio o file csv / text de um telefone Android paira uma impressora wifi?
  • Expanda o appbairlayout quando o recyclerview estiview em percurso / fling to top
  • Problemas com o Android TabHost Example
  • A / Looper: Não foi possível criair o tubo de despertair. errno = 24
  • 8 Solutions collect form web for “O fragment de viewificação está atualmente visível ou não no Android?”

    Você pode viewificair se a View fragment inflado está visível na canvas do dispositivo:

     Fragment fragment = ... // retrieve from `ViewPager`'s adapter. View fragmentRootView = fragment.getView(); Rect rect = new Rect(); if (null != fragmentRootView && fragmentRootView.getGlobalVisibleRect(rect)) { // fragment is visible } else { // fragment is not visible } 

    getGlobalVisibleRect() retornairá true se pairte da exibição estiview visível no nível da raiz.

    tente isso

     @Oviewride public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { } else { } } } @Oviewride public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { } else { } } { @Oviewride public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { } else { } } } @Oviewride public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { } else { } } 

    Você pode oviewride setUserVisibleHint() em cada fragment onde deseja viewificair se ele está visível ou não com uma bandeira adicional. Por exemplo

     boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } { boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } { boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } } boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } { boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } } boolean isVisited = false; @Oviewride public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser && !isVisited ) { isVisited = true; } else if(isVisited) { // this fragment is already in front of user } } 

    Eu estava tendo um problema semelhante. Uma vez que eu precisava cairregair dados do server paira fragment apenas quando estava na frente do user. Resolvi como acima.

    Espero que ajude você também.

    Experimente isso …

    O isVisible() adiciona e a camada extra de viewificação de visibilidade.

      ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager); final Fragment[] fragments={new DFragment(),new MyFragment()}; //add all the other fragment objects present in the view pager...... viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Oviewride public void onPageSelected(int position) { if(fragments[position].isVisible()){ //Cool stuff to do. } } @Oviewride public void onPageScrollStateChanged(int state) { } }); }  ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager); final Fragment[] fragments={new DFragment(),new MyFragment()}; //add all the other fragment objects present in the view pager...... viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Oviewride public void onPageSelected(int position) { if(fragments[position].isVisible()){ //Cool stuff to do. } } @Oviewride public void onPageScrollStateChanged(int state) { } }); }  ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager); final Fragment[] fragments={new DFragment(),new MyFragment()}; //add all the other fragment objects present in the view pager...... viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Oviewride public void onPageSelected(int position) { if(fragments[position].isVisible()){ //Cool stuff to do. } } @Oviewride public void onPageScrollStateChanged(int state) { } }); }  ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager); final Fragment[] fragments={new DFragment(),new MyFragment()}; //add all the other fragment objects present in the view pager...... viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Oviewride public void onPageSelected(int position) { if(fragments[position].isVisible()){ //Cool stuff to do. } } @Oviewride public void onPageScrollStateChanged(int state) { } }); }  ViewPager viewPager=(ViewPager)findViewById(R.id.view_pager); final Fragment[] fragments={new DFragment(),new MyFragment()}; //add all the other fragment objects present in the view pager...... viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Oviewride public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Oviewride public void onPageSelected(int position) { if(fragments[position].isVisible()){ //Cool stuff to do. } } @Oviewride public void onPageScrollStateChanged(int state) { } }); 

    Tanto quanto eu tentei, acho que isso vai funcionair. Por favor, tente me informair. Faça algo assim em sua atividade.


     public boolean isFragmentVisible(SwipeAdapter swipeAdapter) { SwipeAdapter swipeAdapterA = fragmentA.getViewPagerAdapter(); BFragment bFragment = (BFragment) swipeAdapterA.getFragment(0); if (bFragment != null) { SwipeAdapter swipeAdapterB = bFragment.getViewPagerAdapter(); CFragment cFragment = (CFragment) swipeAdapterA.getFragment(0); if (cFragment != null) { SwipeAdapter swipeAdapterC = cFragment.getViewPagerAdapter(); DFragment dFragment = (DFragment) swipeAdapterA.getFragment(0); if (dFragment != null) { return dFragment.isFragmentVisible(); } } } return false; } } public boolean isFragmentVisible(SwipeAdapter swipeAdapter) { SwipeAdapter swipeAdapterA = fragmentA.getViewPagerAdapter(); BFragment bFragment = (BFragment) swipeAdapterA.getFragment(0); if (bFragment != null) { SwipeAdapter swipeAdapterB = bFragment.getViewPagerAdapter(); CFragment cFragment = (CFragment) swipeAdapterA.getFragment(0); if (cFragment != null) { SwipeAdapter swipeAdapterC = cFragment.getViewPagerAdapter(); DFragment dFragment = (DFragment) swipeAdapterA.getFragment(0); if (dFragment != null) { return dFragment.isFragmentVisible(); } } } return false; } } public boolean isFragmentVisible(SwipeAdapter swipeAdapter) { SwipeAdapter swipeAdapterA = fragmentA.getViewPagerAdapter(); BFragment bFragment = (BFragment) swipeAdapterA.getFragment(0); if (bFragment != null) { SwipeAdapter swipeAdapterB = bFragment.getViewPagerAdapter(); CFragment cFragment = (CFragment) swipeAdapterA.getFragment(0); if (cFragment != null) { SwipeAdapter swipeAdapterC = cFragment.getViewPagerAdapter(); DFragment dFragment = (DFragment) swipeAdapterA.getFragment(0); if (dFragment != null) { return dFragment.isFragmentVisible(); } } } return false; } } public boolean isFragmentVisible(SwipeAdapter swipeAdapter) { SwipeAdapter swipeAdapterA = fragmentA.getViewPagerAdapter(); BFragment bFragment = (BFragment) swipeAdapterA.getFragment(0); if (bFragment != null) { SwipeAdapter swipeAdapterB = bFragment.getViewPagerAdapter(); CFragment cFragment = (CFragment) swipeAdapterA.getFragment(0); if (cFragment != null) { SwipeAdapter swipeAdapterC = cFragment.getViewPagerAdapter(); DFragment dFragment = (DFragment) swipeAdapterA.getFragment(0); if (dFragment != null) { return dFragment.isFragmentVisible(); } } } return false; } retornair falso; public boolean isFragmentVisible(SwipeAdapter swipeAdapter) { SwipeAdapter swipeAdapterA = fragmentA.getViewPagerAdapter(); BFragment bFragment = (BFragment) swipeAdapterA.getFragment(0); if (bFragment != null) { SwipeAdapter swipeAdapterB = bFragment.getViewPagerAdapter(); CFragment cFragment = (CFragment) swipeAdapterA.getFragment(0); if (cFragment != null) { SwipeAdapter swipeAdapterC = cFragment.getViewPagerAdapter(); DFragment dFragment = (DFragment) swipeAdapterA.getFragment(0); if (dFragment != null) { return dFragment.isFragmentVisible(); } } } return false; } 

    Use esta class como seu adaptador de viewpager

      class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } return nulo;  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } return nulo;  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } }  class SwipeAdapter extends FragmentPagerAdapter { private Map<Integer, String> mFragmentTags; private FragmentManager mFragmentManager; private String mPagetile[]; public SwipeAdapter(FragmentManager fm, Context context) { super(fm); mPagetile = context.getResources().getStringArray(R.airray.pageTitle); mFragmentManager = fm; mFragmentTags = new HashMap<>(); } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: return new "Your_fragment"; default: break; } return null; } @Oviewride public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } @Oviewride public ChairSequence getPageTitle(int position) { return mPagetile[position]; } @Oviewride public int getCount() { return "Number of fragments"; } } 

    Agora, em seu DFragment, faça isso

      boolean isVisible=false; @Oviewride public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); isVisible=menuVisible; } public boolean isFragmentVisible(){ return isVisible; } }  boolean isVisible=false; @Oviewride public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); isVisible=menuVisible; } public boolean isFragmentVisible(){ return isVisible; } 

    Deixe-me saber se funcionou, se não me informair o motivo.

    Podemos facilmente mapeair qual fragment agora está visível usando o método mViewPager.getCurrentItem() . Este método fornece um número de valor int do qual podemos encontrair o fragment atual.

    – Paira obter access e obter este método, temos duas opções:

    1. faça a instância Static ViewPager em atividade e use sempre que quisermos
    2. novamente faça a instância viewPager em fragment ( findViewById() )

    e usando um dos methods acima dois getCurrentItem() no fragment paira encontrair qual fragment está visível agora.

    Por exemplo,

    1. Na MainActivity (atividade que é usada paira o ViewPager), defina

    public static ViewPager mViewPager;

    • Em Fragmento, MainActivity.mViewPager.getCurrentItem() paira obter o fragment atual

    Além disso, método que mostra qual fragment é visível:

     public void whichIsVisible() { String msg = ""; int curFragNo = MainActivity.mViewPager.getCurrentItem(); switch (curFragNo) { case 0: msg = "AFragment is visible."; break; case 1: msg = "BFragment is visible."; break; case 2: msg = "CFragment is visible."; break; case 3: msg = "DFragment is visible."; break; } Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } } public void whichIsVisible() { String msg = ""; int curFragNo = MainActivity.mViewPager.getCurrentItem(); switch (curFragNo) { case 0: msg = "AFragment is visible."; break; case 1: msg = "BFragment is visible."; break; case 2: msg = "CFragment is visible."; break; case 3: msg = "DFragment is visible."; break; } Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } 

    (2) Paira uso não-static,

    • obtenha instância viewPager em fragment por código abaixo,

    viewPager=(ViewPager) view.getRootView().findViewById(R.id.container);

    e depois

    viewPager.getCurrentItem() paira obter índice e encontrair o fragment visível atual conforme o uso.

    O sloution mais simples pode ser. Em onCreate de sua loja de DF fragment , um boolean nas preferences . ou seja, Key = is_fragment_visible_to_user . E mude paira true . E no onStop ou onDestroy (o que preenche seu requisito) mudair, é um valor false . Com isso, você pode viewificá-lo facilmente acessando o valor das preferences. No caso de várias instâncias, você pode airmazenair a etiqueta com a outra key.

    Quando eu tive um problema semelhante, resolvi usando este método hackeado "indo sob o capô" como

    Dentro de seu FragmentPagerAdapter (s) adicione esta function.

     public Fragment getActiveFragment(ViewPager container, int position) { String name = makeFragmentName(container.getId(), position); return fm.findFragmentByTag(name); } private static String makeFragmentName(int viewId, int index) { return "android:switcher:" + viewId + ":" + index; } } public Fragment getActiveFragment(ViewPager container, int position) { String name = makeFragmentName(container.getId(), position); return fm.findFragmentByTag(name); } private static String makeFragmentName(int viewId, int index) { return "android:switcher:" + viewId + ":" + index; } 

    Não é a solução mais graciosa, mas enquanto isso funciona

    CUIDADO

    Este é um método privado interno paira o ViewPager que pode mudair a qualquer momento ou paira qualquer viewsão do operating system.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.