Search code examples
androidandroid-appcompatappcompatactivity

How do I properly set the content view in AppCompatActivity?


When I call the setContentView(myView) method in an AppCompatActivity, instead of setting it directly, it calls through to some sort of delegate which for some reason wraps it in some unneeded layouts. However, I need it to be a direct child of the decor view. I've already disabled the action bar and window title via theme, unfortunately that didn't help. If I do ((ViewGroup)getWindow().getDecorView()).addView(myView) the app crashes immediately on launch when said delegate tries to do something with its unneeded layouts and finds my view instead of them. The worst part is that such a behavior is absolutely undocumented, I found that out myself while debugging trying to understand why does the system disregard setFitsSystemWindows(true) on my root content view (which is a DrawerLayout).

Is there any way to completely disable this behavior? If no, is there any way to use the system Activity class while using the appcompat layout inflater in order to support per-view themes on pre-Lollipop?

I don't support anything older than ICS and I use toolbars inside fragments (without setting them as action bars) so I don't actually need any of appcompat's activity-related stuff in my app; the only reason to extend AppCompatActivity for me was its layout inflater.


Solution

  • Have you tried AppCompatDelegate? It's a helper that provides you with compat behavior without requiring you to extend AppCompatActivity. However, it may well apply same wrapping pattern to your layouts (haven't tried it myself), but anyway worth taking a look.