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
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().
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);
}
MyActivity extends Activity
==> WorkedMyActivity 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With