Access ActionBar from fragment

How do you access the ActionBar from a fragment (android.support.v4.app.Fragment file). I am using Android v4 and v7 support library. I do not use the Sherlock library.

The activity hosting the fragment is ActionBarActivity.

I read the Android Fragment help section, which also led me to getActivity (), but there is no getSupportActionBar () method.

ActionBar actionBar = getActivity().getSupportActionBar() 
+7
java android android-fragments android-support-library
source share
2 answers

I read the Android Fragment help topic, which also led me to getActivity (), but there is no getSupportActionBar () method.

You will need to specify the result of getActivity() as ActionBarActivity , on which you will find getSupportActionBar() .

+21
source share

If you are using AppCompatActivity then do it like this. ((AppCompatActivity )getActivity()).getSupportActionBar();

It is also recommended that you go to Toolbar using AppCompatActivity instead of the action bar (application bar). For more information, refer to http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html

+7
source share

All Articles