Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Toolbar in Fragment or in Activity

I am using Android-ObservableScrollView library. Everything works great, but I have activity for holding fragments, so all views are encapsulated in the fragment. In activity there is only FrameLayout for holding fragments.
So I need to use Toolbar in my application, I have several ideas how to implement this.

  1. Use Toolbar in activity, in this case my layout will have FrameLayout and Toolbar. In this way I have communicate with activity whenever I need to do something with toolbar, I can also obtain it by using getSupportedActionBar() from fragment.

  2. Use Toolbar inside fragment (in its layout) setting in each fragment view creation. And each time I change fragment I have to add new Toolbar to the activity. In some fragment I am going to have different toolbars but not in all. Is it good approach to store Toolbar inside fragment.

The problem that I can see in using second approach, if there will be more than one fragment on the screen there will be also several toolbars.

Please suggest what will be the right way in this case.
Thank you.

like image 913
CROSP Avatar asked May 30 '15 12:05

CROSP


1 Answers

You should use first method. While using first method will have less problem then second one because in second method you have to define toolbar for many times which is not good programming.

like image 59
Jaydev Desai Avatar answered Oct 07 '22 22:10

Jaydev Desai