Skip to content Skip to sidebar Skip to footer

Detect When A Fragment Is Drawn?

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

Solution 1:

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(newRunnable() {
    @Overridepublicvoidrun() {
      // code you want to run when view is visible for the first time
    }
  }
)

Solution 2:

onWindowFocusChanged(boolean hasFocus) method calls in activity, but when fragment drawns we can check through method onActivitycreated(). Check this post https://stackoverflow.com/a/32819850/2732632

Solution 3:

OnWindowFocusChanged doesn't provide info about drawing a view (or didn't see) Implement OnWindowFocusChanged on each view that can have focus in your Fragment. in pseudo-code:

FragmentimplementsOnWindowFocusChanged{

onCreate(){

btn.OnWindowFocusChanged(this);
editTxt.OnWindowFocusChanged(this);
....

}

publicvoidOnWindowFocusChanged(boolean hasFocus){
// your fragment has focus
}

}

Post a Comment for "Detect When A Fragment Is Drawn?"