25

Why not a duplicate: Suggested post only works for a viewpager, or when onResume is called.

I have a listfragment and a detailfragment. The detail fragment is opened when a list item is clicked. I accomplish this by hiding the listfragment and I showing the detailfragment. When the user goes back, he returns to the listfragment.

How can I detect when the user gets back to my fragment, or when my fragment is visible?

Please note that I would like to keep using .hide() and show() and that I am looking for a listener or a onVisible to check when the fragment becomes visible, and not a method to check if it is visible.

Mdlc
  • 7,128
  • 12
  • 55
  • 98
  • Is there any particular reason you need to use hide and show?. I would use Fragment Transactions and just check the stack count, e.g. If it's 1 it means your detail fragment is visible, if not, the listfragment is. – elmango Feb 12 '14 at 16:37
  • Why do you prefer to use hide() and show() instead of fragment transactions and back stack? It might be easier to accomplish what you need following the guidelines... – stan0 Feb 12 '14 at 16:44
  • @Elenasys This applies for a viewpager where setuservisiblehint can be used – Mdlc Feb 12 '14 at 17:57
  • 2
    Does `onHiddenChanged()` fire for your fragments? – super-qua Feb 12 '14 at 18:11
  • If you could provide me some info on how to use to detect if my fragment is visible again, that would be awesome. I'll check if it gets called asop, but I feel think this might work! – Mdlc Feb 12 '14 at 18:19
  • According to the documentation http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean) it will be called if the hidden state changes. Beware because it states that it will not be called on the first start of the fragment, but on subsequent state changes – super-qua Feb 12 '14 at 18:22
  • Let me know if it works, in case it does I'll edit my answer for others to see in the future. – super-qua Feb 12 '14 at 18:36
  • Yes, it gets called. When I open detailfragment from listfragment & when I go back to listfragment. So I'll then I'll only have to check if the fragment is currently visible. – Mdlc Feb 12 '14 at 19:06
  • Ok great. I edited my answer so all information is in one place. – super-qua Feb 13 '14 at 08:48

3 Answers3

28

EDIT:
As pointed out in the comments, setUserVisbileHint() does not get called automatically, but by the FragmentPageAdapter when used in a ViewPager.

For the scenario described in the question, onHiddenChanged(boolean hidden) http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean) is suitable.
As stated in the documentation, the method gets called every time the Fragments hidden state changes, but not on the initial setup of the Fragment.


You could use setUserVisibleHint() http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)

if isVisibleToUseris set to truethe fragment should be displayed.

super-qua
  • 3,148
  • 1
  • 23
  • 30
  • 1
    This method gets called once the Fragment becomes visible. I use it in a ViewPager. – super-qua Feb 12 '14 at 17:45
  • http://stackoverflow.com/questions/14731589/is-fragment-setuservisiblehint-called-by-the-android-system - "the answer to the question clearly: you call it manually, and FragmentPagerAdapter also calls it manually" – Mdlc Feb 12 '14 at 17:47
  • Ok thanks, didn't know that the FragmentPagerAdapter calls it. Learned something. – super-qua Feb 12 '14 at 17:50
-1

Read about the method : Fragment. isVisible()

Example:

MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentById("myFragnmentID");
if (test != null && test.isVisible()) {
     //VISIBLE! =)
}
else {
    //NOT VISIBLE =(
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • This is to check if it is visible (by calling this code). But I would like to be notified when the fragment becomes visible – Mdlc Feb 12 '14 at 16:45
  • http://stackoverflow.com/questions/14731589/is-fragment-setuservisiblehint-called-by-the-android-system – Mdlc Feb 12 '14 at 17:54
  • 2
    neighbors in the view pager are always reported as visible – OneWorld Mar 22 '16 at 12:46
-2

You could send an event to your activity when the fragment is started : https://www.inkling.com/read/programming-android-mednieks-1st/chapter-11/visualizing-the-fragment-life

By overriding the onStart method :

@Override
public void onStart() {
    super.onStart();
    ((MyInterface)getActivity()).onFragmentIsVisible( this );
}

Where MyInterfaceis an interface :

public MyInterface {
   public void onFragmentVisible( Fragment fragment );
}

and your activity will have to implement it.

This could be more easy with an event bus library like

JAM
  • 6,045
  • 1
  • 32
  • 48
Snicolas
  • 37,840
  • 15
  • 114
  • 173