1

I'm trying to create an application that uses fragments, but when I try to show a Fragment in a FragmentActivity the action bar doesn't appear.

FragmentActivity class:

public class NoteDetailsActivity extends FragmentActivity {

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


        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            NoteDetailsFragment details = new NoteDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.note_details, details).commit();
        }
    }
}

Fragment class:

public class NoteDetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static NoteDetailsFragment newInstance(int index) {
        NoteDetailsFragment f = new NoteDetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        int index = getArguments().getInt("index");
        String message = "";
        switch (index) {
            case 0:
                message = "Start";
                break;
            case 1:
                message = "Second";
                break;
            case 2:
                message = "last";
                break;

        }

        TextView text = new TextView(getActivity());
        text.setText(message);
        return text;
    }
}

I have tried to implement the onCreateOptionsMenu in my FragmentActivity class but nothing happens. I've also tried to setHasOptionsMenu(true) in the Fragment class, but that doesn't do anything either.

If it's of interest, here's they layout where I insert my Fragment (note_details_fragment):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_above="@id/save_note"
        android:id="@+id/note_details"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" />

    <Button
        android:id="@+id/save_note"
        android:layout_alignParentBottom="true"
        android:text="@string/save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

EDIT: From manifest:

    <activity android:name=".activities.NoteDetailsActivity">
    </activity>
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Please show the manifest declaration of "NoteDetailsActivity"... – Martin Pfeffer Jan 21 '15 at 16:55
  • It's included at the bottom @MartinPfeffer – Marcus Jan 21 '15 at 16:59
  • the manifest? are you sure? I just see the xml of the Fragment... You have to declare every activity in the manifest, otherwise it wouldn't start... – Martin Pfeffer Jan 21 '15 at 17:02
  • Sorry, I misunderstood. Check my edit. @MartinPfeffer – Marcus Jan 21 '15 at 17:03
  • Okay, I'm not completely sure if this is the issue, but check this page (maybe the following pages of the docs, too - they are quite important): https://developer.android.com/training/basics/actionbar/setting-up.html maybe you have you change the "host-activity" to extending the ActionBar and nest the fragment in it... This is the way I set up the action bar in my app (using fragments). – Martin Pfeffer Jan 21 '15 at 17:06

1 Answers1

2

Deriving from class ActionBarActivity instead of FragmentAcivity should solve your problem:

public class NoteDetailsActivity extends ActionBarActivity {
   // ...
}
skywall
  • 3,956
  • 1
  • 34
  • 52
  • 1
    `ActionBarActivity` is deprecated. `android.support.v7.app.AppCompatActivity` works for me - but I haven't tried it with fragments yet... – ban-geoengineering Jan 05 '16 at 16:01
  • I've just tested myself, `AppCompatActivity` works with fragments as `FragmentActivity` would. Just remember to include in your gradle dependencies: `implementation 'com.android.support:appcompat-v7:26.1.0'` – ParKein Dec 09 '17 at 10:29