I have to load a font with callbacks and set it as the theme typeface with a reflection method. And this has to be done before the setContentView()
in onCreate()
is called.
I tried to use a CountDownLatch
on the main thread as below to make it wait for the font loading process:
loadFont();
try {
System.out.println("latch awaiting");
latch.await();
} catch (InterruptedException e) {
System.out.println("latch error");
e.printStackTrace();
} finally {
System.out.println("latch error");
latch.countDown();
}
But it appears that the callback is initiated, but never returned any result as it normally would(onTypefaceRetrieved()
not called). The new thread and callbacksks are initiated as below:
Runnable runnable = new Runnable(){
@Override
public void run() {
FontCall.Callback callback = new FontCall.FontCallback(){
@Override
public void onFontRetrieved(final Typeface typeface) {
//do reflection stuff
latch.countDown();
}
@Override
public void onError(int err) {
//handle error
latch.countDown();
}
};
FontCall.requestFont(callback);
}
};
Thread thread = new Thread(runnable);
thread.run();
Can someone tell me what I did wrong? Or am I not supposed to do this kind of thing (make UI thread wait) at all? (For reference, the font is usually retrieved from Android's font cache, which takes about less than 100ms.)