as container for Fragment

My situation is Activity A which contains Fragment B. I always implement it like this.

Layout for Activity A:

  • How to display my location on Google Maps for Android API v2
  • How to Build a RSS reader for Android?
  • How do I draw an arrowhead (in Android)?
  • java.lang.RuntimeException: Only one Looper may be created per thread
  • Icon in Tab is not showing up
  • How can I set the color of android rating bar's stroke? (Not the color of the stars but the BORDER)
  • <?xml version="1.0" encoding="utf-8"?>
        android:layout_height="match_parent" />

    Layout for Fragment B:

    <?xml version="1.0" encoding="utf-8"?>

    This works great, but if we open Android Device monitor and look at View Hierarchy:

    So, I do not like that in my hierarchy there are two same useless FrameLayouts and I can cut my I do it like this:

    onCreate(Bundle args) implementation in my Activity A:

    protected void onCreate(Bundle savedInstanceState) {
            .add(, FragmentB.newInstance()).commit();

    I just do not set content for my Activity and attach my Fragment B to system container This works great for me. I removed one useless include.

    My question is it good practice to do this “hack”. Could it crashs my application in any cases and what problems could I have after this implementation? May be somebody has useful experience in this question?

    Thanks to all for good answers.

  • Android Studio Espresso Testing Error: Empty Test Suite
  • How do android screen coordinates work?
  • Using TabLayout inside a Fragment; tab text invisible
  • how to design right to left linear layout
  • Android, how to not destroy the activity when I rotate the device?
  • How to programmatically change contrast of a bitmap in android?
  • One Solution collect form web for “ as container for Fragment”

    there is nothing wrong with it. Like you said: you dont need your extra layout so… just don’t add it with setContentView. There is even mention about it in official documentation of ActionBar:

    Alternatively, if the tab content will fill the activity layout, then
    your activity doesn’t need a layout at all (you don’t even need to
    call setContentView()). Instead, you can place each fragment in the
    default root view, which you can refer to with the ID

    If you develop only for 14+ (because of native ActionBar) everything should be fine with it, but if you use support lib please read the points below.

    I. If you use Support Library revision lower than 19:

    Important thing is: What is your min API level that you develop for?

    If your app supporting API < 14 and you use AppCompat you must be aware the different behavior. is the part of screen where your application should display it’s content.
    On native API 14+ This is just part below ActionBar, because this part is supposed to display activity content.

    In AppCompat, where there is no native support for ActionBar. is the container of entire app screen. This means – including ActionBar, because ActionBar is emulated there and added as a standard view hierarchy. To solve this issue you have to check whether you are on API lower than 14 and use different id:

    You can create helper method to get correct id:

    public static int getContentViewId() {

    So if you are developing for 14+ this is perfectly fine solution. If you use custom ActionBar implementation (like AppCompat) you have to do this trick.

    II. If you use Support Library revision 19 (or greater):

    It seems that this behavior was fixed in Support Library revision 19:

    You can see that they replacing the old with standard (and the old with NO_ID) for better compatibility! So if you use Support Lib r19 or greater (or just a native framework) you can just just in both <14 and 14+ variants:)

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.