15
android.util.AndroidRuntimeException: requestFeature() must be called before adding content

I get this error when i use

getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

from fragment. I want to change actionbar style only in this fragment. So I can't set this in MainActivity. How to solve this?

I saw this question requestFeature() must be called before adding content it does not say how to solve this issue from a fragment

Community
  • 1
  • 1
Jithin Sunny
  • 3,352
  • 4
  • 26
  • 42
  • 2
    before you call setContentView() you should put `getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);` – Sagar Pilkhwal Sep 26 '14 at 05:58
  • http://stackoverflow.com/questions/4250149/requestfeature-must-be-called-before-adding-content – raja Sep 26 '14 at 05:59

5 Answers5

8

I also got this error, working with a DialogFragment, even though I wasn't calling requestFeature() at all.

I was calling getDecorView() from the DialogFragment's onActivitiyCreate() as part of some tracing code I had written to help me understand how and when Windows are created. That worked fine, but a bit later in the fragment's life cycle its onStart() method was called. That called Dialog's show() which eventually called AlertDialog's onCreate() which eventually called PhoneWindow's requestFeature() method to request Window.FEATURE_NO_TITLE.

Since calling getDecorView() "for the first time 'locks in' various window characteristics as described in setContentView(View, android.view.ViewGroup.LayoutParams).", this violated the requirement that "requestFeature() gets called before adding content in Fragment" -- the subtlety being that the content was getting added indirectly by my call to getDecorView().

The fix was to call peekDecorView() instead of getDecorView().

Barry Holroyd
  • 1,005
  • 1
  • 11
  • 20
  • 4
    Just wow. You basically saved my live as it seems. We had a bug in production that crashed the app with the "requestFeature()" stracktrace. We weren't able to track it down the past two days- until I stumbled across your post. We were also calling getDecorView on the DialogFragment. I changed that and I guess it works! I'm still not 100% sure if this really fixes the whole issue in every possible way in our case, but it's seems like we are on a good way! Thank you. – battlepope Sep 28 '16 at 07:50
6

Regardless of whatever people are responding this issues still appear if you use AppCompatActivity as a parent to your activity.

For me this code throws error: ​

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.activity_browser);
}
  • Test 1 : MyActivity extends Activity ==> Worked
  • Test 2 : MyActivity extends AppCompatActivity ==> Error "requestFeature() must be called before adding content in activity"

The solution for Test 2 (if you are using Appcompat) is to call requestFeature before super.onCreate. It would solve your issue.

Adam S
  • 16,144
  • 6
  • 54
  • 81
Irfan Raza
  • 2,859
  • 1
  • 22
  • 29
2

You have to call getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); before the setContentView() in the Activity's onCreate() method. You have to add this line in your FragmentActivity from where your Fragment is being called.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • There is more than one fragment.I want this feature only in a particular fragment.So if set this feature in the Parent activity all other fragments have this feature – Jithin Sunny Sep 26 '14 at 06:09
  • @Sunny for that you have to make custom theme and apply it to your fragment class – Piyush Sep 26 '14 at 06:16
2

requestFeature() should be called before setContentView() in your activity. Calling getActivity().getWindow().requestFeature() from Fragment is bad practice. If you want your action bar visibility to be delayed, i will recommend to hide actionbar in onCreate() of your activity & unhide it in onViewCreated() in your fragment.

Akhil
  • 6,667
  • 4
  • 31
  • 61
2

You need to create a separate Activity for this fragment and then requestFeature() on that activity before setContentView() because requestFeature() is method of android.view.Window. Window features are for the specific activity. You can manipulate the window behaviors for a specific activity not specific fragment.

Umair Khan
  • 193
  • 1
  • 2
  • 11