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?
Asked
Active
Viewed 6.2k times
27
-
6It is not really a duplicated because the other question is asked in the context of a ViewPager, and the answers reflect that context. This is a more general question. – awy Aug 20 '14 at 14:53
-
3this is not a duplicate. – Jayson Tamayo Aug 30 '14 at 13:20
-
The solution that worked best for me was http://stackoverflow.com/a/6751537/1816603 – Jose Gómez Jul 04 '15 at 22:37
3 Answers
54
check if(YourFragment.this.isVisible())

ashokramcse
- 2,841
- 2
- 19
- 41

mrsus
- 5,446
- 4
- 31
- 44
-
1How to in a Tabbed Action Bar? Navigation is messed up in this fragment scheme. – Skynet Dec 05 '13 at 05:06
-
-
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
-
-
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
-
This returns false if i call it from the service, but maybe i'm doing something wrong. – user2538848 Jul 01 '13 at 14:09
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.