Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a fragment is drawn?

Tags:

android

Basically I need the functionality of onWindowFocusChanged() but on a Fragment. The onWindowFocusChanged() listener isn't actually available to fragments unfortunately, so I'm not sure what to do.

Any way to do this?

like image 495
VicVu Avatar asked Nov 06 '12 16:11

VicVu


People also ask

How do you identify a fragment?

A fragment resembles a sentence in two ways. Both groups of words begin with a capital letter and conclude with an end mark—usually a period ( . ) but sometimes a question mark ( ? ) or an exclamation point ( ! ). The one important difference is that a fragment does not contain a main clause.

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.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.

How do you 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.


1 Answers

Fragments are "drawn" via the View you return in onCreateView(). You can use the event handlers in that. Usually, if there's a command that needs to be run when the View first comes to view, you can assign it in a Runnable and attach it to the View via the post() method.

getView().post(new Runnable() {     @Override     public void run() {       // code you want to run when view is visible for the first time     }   } ) 
like image 165
DeeV Avatar answered Oct 12 '22 00:10

DeeV