44660

Android Activity.onWindowFocusChanged doesn't get called from within TabHost

I'm struggling with the problem that the .onWindowFocusChanged() doesn't get called in my custom Activity class. My setup:

Two tabs (containing Activity_1 and Activity_2) in a TabHost, where the 2nd tab is selected by default:

tabHost.setCurrentTab(currentTabIndex);

In both Activities, I added the onWindowFocusChanged() override (because I need to preform calculations after the layout is done drawing):

@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); }

The problem: when the 2nd tab is selected by default, and I click the 1st tab, the onWindowFocusChanged() never gets called within Activity_1 (associated with the 1st tab). Both Activities extend the normal Activity class.

Any clue on how to fix this would be greatly appreciated!

Answer1:

If you need to wait until a specific View is draw and then make the calculations, you could use viewTreeObserver to listen the layout changes and make your calculations there. Use it like this:

ViewTreeObserver vto = mainLayout.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { // remove the listener so it won't get called again if the view layout changes mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); // add your calculations here }}

<strong>EDIT:</strong>

Credit and thanks to Edison for the further details.

For those who want to support <strong>API < 16</strong>, you can do

if (Build.VERSION.SDK_INT >= 16) { mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); }

Recommend

  • Protractor Drag And Drop
  • reformat multidimensional array based on value
  • Angular does not updated loop due to implementation $mdDialog (Material Design)
  • Assembly program crashes on call or exit
  • How can I display changed line in Emacs?
  • Complex conditional element in accumulate in Jess rules
  • Why is my text not being added to the textarea after the second change event?
  • How to get a specific type object from an Object Vector in Java
  • replacement of asp:Treeview for ASP.NET MVC application
  • IllegalArgumentException: the error message does not make sense
  • Making data obtained in one activity available to all the activities
  • Stop service on swipe to remove
  • How to create a waiting page in Django
  • Matlab code optimization and removing loops [closed]
  • How can I do some cleanup right before an Android app is quitting?
  • What's the best way to have reusable dialog boxes?
  • Ordering of eigenvectors when calculating eigenvectors using LAPACK's ssteqr
  • How to remove fragment from fragment manager when activity is restored after being destroyed
  • how to use asyncio with boost.python?
  • Changing the user agent in Chromium Embedded 3 (DCEF3) (CefVCL)
  • Using HTML/CSS for UI in XNA?
  • c# winform DrawToBitmap offscreen
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • WPF version of .ScaleControl?
  • Implicit joins and Where in Doctrine - how?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Adding elements to a huge XML file
  • Debug.DrawLine not showing in the GameView
  • How to define and use opencv mat of user type
  • Cassandra Data Model
  • align graphs with different xlab
  • php design question - will a Helper help here?
  • Getting Messege Twice Using IMvxMessenger
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • Python/Django TangoWithDjango Models and Databases
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize