I'm struggling with StackOverflowErrors in the main UI thread (related question). My app targets Android 2.3+. On Android 4 everything is fine, but I'm getting crashes on 2.3.3 when the view layout is drawn.
The obvious answer is to optimize my view layout and reduce the number of nested views. This is a problem because the large number of views is due to me using the support library and fragments for tabs - which is the recommended approach. I don't want to change my design and drop fragments simply because an older ver of the OS has a small stack.
I'm looking for ways to increase the stack size of the UI thread. I've seen answers that this is not possible, and I understand that there is no simple setting in the manifest to do so. But, I'm still not satisfied that there is no manual workaround.
My best idea thus far:
Create a new worker thread (Thread class) with a larger stack. It is possible to increase the stack size for worker threads. Then, transform this thread somehow to the new UI thread. How?
Browsing through the Android sources for android.app.Activity.attach(..) I've seen that an activity attaches itself to a UI thread. Maybe there is some way to replace the thread it is attached to and change it to my "new" UI thread
Going a little deeper to android.app.ActivityThread.startActivityNow(..), maybe I'll be able to start my activity manually. Once the activity is created, it attaches itself automatically to the thread it is created on. Maybe if I run this from my new UI thread, it will work.
If I create a new UI thread, I will have to manually create the Looper for it. I'm getting a sense of what's need to be created from android.app.ActivityThread.main(..)
Other ideas:
Catch the StackOverflowError manually and when it happens move calls asynchronously so they have a smaller stack. I've seen this nice idea here but I was unable to make it work.
NDK threads (via JNI) have large stacks. I thought about using them for my benefit but I see no way to do this. All calls via JNI are performed on their own thread so I can't see how I can use NDK threads for UI access.
So..
Except for saying that this is a very bad idea altogether, any other suggestions or tips? Can you find anyone who did something similar? I couldn't..