0

I want to hide my MainActiviy Toolbar in my fragment, i'm using:

getActivity().findViewById(R.id.appToolbar).setVisibility(View.GONE);

and:

((AppCompatActivity) getActivity()).getSupportActionBar().hide();

and is not working. My fragment has his own Toolbar, and is already been showed, but my Activity Toolbar is being showed too. What am i doing wrong? I just want to show my fragment ToolBar

JaviSanchezG
  • 143
  • 1
  • 12
  • you should use the activity which loads the Fragment, (which has actionBar), for example if you are loading fragment in HomeActivity. try ((HomeActivity) getActivity().getSupportActionBar().hide(); – aadi53 Nov 12 '15 at 21:24
  • is not working... same issue :-( . I try this code in other fragment and it works, the difference between one fragment and other is that when i replace it, i'm adding one into backstack and the other is not being added... (the toolbar disappear when the fragment is not added to th backstack) – JaviSanchezG Nov 12 '15 at 21:33
  • It would be great if you can upload some code snippet, it will help to analyze the issue. – aadi53 Nov 12 '15 at 21:40
  • Actually you can see the full issue in this question http://stackoverflow.com/questions/33658981/fragment-with-collapsingtoolbar-showing-activity-toolbar/33660875 – JaviSanchezG Nov 12 '15 at 21:44
  • Show your code and xml? – T D Nguyen Nov 13 '15 at 21:09
  • Actually the issue was that i am adding the fragment to the backstack, and we cannot delete or hide the SupportActionBar if we have "something" in the backstack, i delete the addToBackStack() method in my project and i am already deleting my supportBar from my Fragment – JaviSanchezG Nov 13 '15 at 21:19

3 Answers3

0

The ActionBar is owned by the Activity not by the Fragment, so you need to call .hide() method on this Activity, try in this way:

getActivity.getSupportActionBar().hide();
Marco
  • 705
  • 8
  • 28
  • getActivity().getSupportActionBar().hide(); cannot be resolved... i have to use ((AppCompatActivity) getActivity()).getSupportActionBar().hide(); – JaviSanchezG Nov 12 '15 at 21:37
0

If you use AppCompat toolbar. Solution:

  1. Must hide Actionbar before using AppCompat Toolbar by apply suitable NoActionBar theme following: this or this
  2. Setup AppCompat toolbar to activity using this guide:

r

Toolbar toolbar = (Toolbar) findViewById(R.id.appToolbar);
setSupportActionBar(toolbar);//replace ActionBar with toolbar
findViewById(R.id.appToolbar).setVisibility(View.GONE);//hide if you want
Community
  • 1
  • 1
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
0

Actually the issue was that i am adding the fragment to the backstack, and we cannot delete or hide the SupportActionBar if we have "something" in the backstack, i delete the addToBackStack() method in my project and i am already deleting my supportBar from my Fragment

JaviSanchezG
  • 143
  • 1
  • 12