27

I have a FragmentActivity, some fragments and a service that is running in background. It is possible to check from the service if a specific fragment is visible?

user2538848
  • 291
  • 1
  • 3
  • 3

3 Answers3

54

check if(YourFragment.this.isVisible())

ashokramcse
  • 2,841
  • 2
  • 19
  • 41
mrsus
  • 5,446
  • 4
  • 31
  • 44
  • 1
    How to in a Tabbed Action Bar? Navigation is messed up in this fragment scheme. – Skynet Dec 05 '13 at 05:06
  • How navigation is messed up? What you want to achieve? – mrsus Dec 05 '13 at 06:14
  • I have an Action Bar tabbed activity, Fragments consist of tabs, I have a button on the Action Bar which once pressed navigates to a different fragment say "Info about the app". As soon as user navigates to info, I disable it so that it does not get called redundantly, then I re-enable it once back is pressed but say if user navigates to info ( info gets disabled until back is pressed) but I want to enable it again if back is not pressed but navigation between tabs occurs. – Skynet Dec 05 '13 at 06:20
  • So far I tried a number of things like referencing the Menu item in the on start and on resume of tab fragments but gives me a null pointer, I am now writing a detailed script and posting as a Question. – Skynet Dec 05 '13 at 06:21
  • yeah, good, you must post question with attempted code. – mrsus Dec 05 '13 at 06:24
  • http://stackoverflow.com/questions/20393016/enable-an-actionbar-button-each-time-navigation-within-tabs-occurs – Skynet Dec 05 '13 at 06:28
21
public boolean isFragmentUIActive() {
    return isAdded() && !isDetached() && !isRemoving();
}

Does the trick.

meredrica
  • 2,563
  • 1
  • 21
  • 24
5

Check the code below:

public class MyFragment extends Fragment
            @Override
            public void setMenuVisibility(final boolean visible) {
                super.setMenuVisibility(visible);
                if (visible) {
                    ...
                }
            }

It's from this thread. Or you can also check this one.

Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43