Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a Fragment is Visible?

I'm using the support library v4 and my questions are, How to know if a Fragment is Visible? and How can I change the propierties of the Layout inflated in the Fragment? Thanks in advance.

---Edit---

I'm using fragments like in the android developers tutorial with a FragmentActivity

like image 291
Poperto Arcadio Buendía Avatar asked May 20 '13 14:05

Poperto Arcadio Buendía


People also ask

How do you know if a fragment is visible?

fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible. To enable this behavior in the first ViewPager you have to pass FragmentPagerAdapter.

How do you get current visible fragments?

To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.

How do I know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

Which method is called once the fragment gets visible?

onStart()The onStart() method is called once the fragment gets visible.


1 Answers

You should be able to do the following:

MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID"); if (test != null && test.isVisible()) {      //DO STUFF } else {     //Whatever } 
like image 121
TronicZomB Avatar answered Sep 19 '22 15:09

TronicZomB