3

I created an ActionBarActivity test app, which shows the title bar and settings menu ok. I changed extends ActionBarActivity to extends FragmentActivity and the program runs, but the title bar / menu isn't displayed.

I am using swipe tabs with ViewPager in my real app, so extending ActionBarActivity isn't an option.

The question was asked similarly in How to Create option menu in FragmentActivity? - but the answer isn't apparent.

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.            
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    >
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
Community
  • 1
  • 1
  • Why does using swipe tabs with ViewPager prevent you from using `ActionBarActivity` (or, as of [yesterday's release of version 22.1](http://android-developers.blogspot.com/2015/04/android-support-library-221.html), `AppCompatActivity`)? `ActionBarActivity`/`AppCompatActivity` extend `FragmentActivity` so you get all of the `FragmentActivity` behavior as well. – ianhanniballake Apr 22 '15 at 20:26
  • My app is laid out with two fragments, a left and right pane. I have a ViewPager on the left pane, and each tab contains more fragments. http://stackoverflow.com/questions/12088983/why-it-is-not-possible-to-use-viewpager-within-a-fragment-it-actually-is – Daniel Lodge Apr 22 '15 at 21:22

2 Answers2

4

The issue is that FragmentActivity doesn't have an ActionBar. The purpose of ActionBarActivity (which is a subclass of FragmentActivity) is to supply the necessary functionality to display an ActionBar in your content. If you do not wish to use ActionBarActivity (or the new and improved AppCompatActivity), use the new class Toolbar, which allows you to embed an ActionBar-like View directly into your layout.

aeskreis
  • 1,923
  • 2
  • 17
  • 24
2

The solution is to simply use ActionBarActivity rather than FragmentActivity.

I confirmed a ViewPager (containing fragments) inside a fragment works fine - contrary to most info on the form.

  • It comes out that ActionBarActivity (though deprecated) is an innocent grandchild of FragmentActivity. Good idea! You may accept your own answer ;-) –  Jun 14 '15 at 15:45