1

Main Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setCustomView(R.layout.action_bar_layout);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        MedSectionFragment medSectionFragment = (MedSectionFragment)getSupportFragmentManager().findFragmentByTag("medSectionFragment");
        if (medSectionFragment == null){
            medSectionFragment = new MedSectionFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(android.R.id.content, medSectionFragment, "medSectionFragment");

            // transaction.addToBackStack(null);
            transaction.commit();
          }
    }

First Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        //Inflate the fragment layout file
        ViewGroup rootView=(ViewGroup)inflater.inflate(R.layout.med_sections_list, container, false);
}

Second Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        //Inflate the fragment layout file
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.official_name_list, container, false);
}

after the transition from one fragment (categories) to another (sub-categories) we have listview with actionbar. its true. enter image description here

But when I transition to the third fragment (detail information)

public class DetailMedicineFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.detail_medicine_fragment, container, false);
        return rootView;
    }
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.mToolbar);
          ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
      }

an error occurs

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99) at ua.edu.sumdu.medmaterialdesign.DetailMedicineFragment.onActivityCreated(DetailMedicineFragment.java:41) at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

application style

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

1 Answers1

1

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead

Since this question already has an answer(with above logcats) and you did that before, it should be done with these two:

<item name="windowActionBar">false</item> 
<item name="windowNoTitle">true</item>

And also, since you've added a Toolbar, make sure the Activity's Styles parent is:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

The point is: NoActionBar.(like i guessed, you were using DarkActionBar)

Also: Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

onActivityCreated():

As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

Sounds good, but, what about using this on onCreate?

getActivity().getSupportActionBar().hide()

It should hide the ActionBar.

But, about this line:

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

I think the problem is in the AppCompatActivity which you are using that in a Fragment i guess.

See: How to get Toolbar from fragment?

And this answer: https://stackoverflow.com/a/26998718/4409113

Something like it's trying to cast from that Activity not for that Fragment only.


And you said: How to remove action bar from fragment?

The thing is, why are you trying to get the Toolbar then?

Remember, Fragments doesn't have Actionbars/Toolbars, Activities has.

http://developer.android.com/guide/components/fragments.html

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108