7

EDIT: If I extend FragmentActivity instead of ActionBarActivity my layout shows up again (without an ActionBar of course).

The ActionBar works as intended on 4.x devices, but on my 2.3 device all I get is the ActionBar and a blank screen below it. The Fragment doesn't seem to be getting added to the Activity.

themes.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="actionBarStyle">@style/Widget.ActionBar</item>
</style>

themes-v11.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/Widget.ActionBar</item>
</style>

styles.xml

<style name="Widget.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@color/actionbar_background</item>
    <item name="background">@color/actionbar_background</item>
</style>

Activity onCreate()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
myFragment = new WallFragment();
fragmentTransaction.add(android.R.id.content, 
    myFragment, myFragment.FRAGMENT_TAG);
fragmentTransaction.commit();

I am using Gradle to include the AppCompat ActionBar library in my app.

compile 'com.android.support:appcompat-v7:18.0.+'
JJD
  • 50,076
  • 60
  • 203
  • 339
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85

1 Answers1

11

Loading up the hierarchyviewer in the Android SDK's tools directory, it appears that the view you place fragments in is android.R.id.content on 4.x devices and is R.id.action_bar_activity_content on 2.3 devices running the AppCompat ActionBar library.

Unfortunately it seems that you might need to branch based off platform versions when adding fragments. This is suggested in http://code.google.com/p/android/issues/detail?id=58108 (not sure about 3.x devices yet).

Use this method to get the proper view for adding Fragment's. My testing also shows that 3.x devices act similar to 2.3 devices when using the AppCompat ActionBar library.

public static int getContentViewCompat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
               android.R.id.content : R.id.action_bar_activity_content;
}


hierarchyviewer screenshots

  • 2.3

2.3

  • 4.x

4.x

Shellum
  • 3,159
  • 2
  • 21
  • 26
  • Another thing to note is that `R.id.action_bar_activity_content` is not created until `setContentView()` is called in the ActionBarActivity. With ABS I was able to use `android.R.id.content` without a layout being set in the `Activity`, this is not the case in Android `2.x`. – Austyn Mahoney Aug 01 '13 at 16:55
  • @Androidy you shouldn't have to do that. Make sure you are using the R file from your application, and not the one from support-v7 when using R.idaction_bar_activity_content. – James McCracken Aug 05 '13 at 21:32
  • That is the weirdest thing, but it fixed the issue. I wonder why the IDE lets you access it from both packages. – Austyn Mahoney Aug 05 '13 at 22:21
  • @AustynMahoney I incorporated getContentViewCompat to my fragment code and moved the fragment code to onCreateOptionsMenu and it started working on 2.3 and 4.0 ??? But I don't like this workaround at all. – danny117 Sep 18 '13 at 16:59
  • 1
    This also fixes the issue when your content seems to appear above the ActionBar on Android 2.3 -- Thanks for noticing that! – Stephan Sep 19 '13 at 08:14
  • 3
    @Shellum If you **update appcompat-v7 to revision 19.0.0.** or newer it is **no longer necessary to switch** between `android.R.id.content` and `R.id.action_bar_activity_content`. The different behavior is unified in the latest version of the library as you can read in the [comment of Chris Banes on issue 59077](https://code.google.com/p/android/issues/detail?id=59077#c8). – JJD Nov 12 '13 at 11:06
  • 1
    It looks like that even after the appcompat-v7 revision 19.0.0 fix, 2.x devices will not add the fragments unless you set the content view (I am just using an empty frame layout). The part about no longer needing R.id.action_bar_activity_content is correct, but on 2.x devices it will silently fail to add fragments. – rrbrambley Nov 13 '13 at 20:20
  • 1
    @rrbrambley Please report this issue on the bug tracker. The original bug has not been closed yet: https://code.google.com/p/android/issues/detail?id=59077 – Austyn Mahoney Dec 04 '13 at 01:03