How do I set theSupportActionBar () in multiple fragments?

I have two pieces of support sitting inside AppCompatActivity

. By design, each of them has a unique toolbar and optional menus. AppCompatActivity

does not have a toolbar in it, as they are included in every snippet.

In each fragment, I am setHasOptionsMenu(true);

in onCreate ().

In onCreateView () I call ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

where the toolbox is the object associated with the xml toolbox item.

In onCreateOptionsMenu()

, I first make a call to super, then I call menu.clear () and finally inflate the menu with inflater.inflate (R.menu.searchbar_menu, menu);

If it fails, it is called ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

in both fragments. In this case, the parameters will be displayed only in the second fragment, and not in the first one. If I name it with just one snippet, the options show up as expected in only one snippet. Of course, if I don't name it in any of them, they won't show up at all.

Here is my code. Both snippets are essentially identical except for the bloated menus:

public class FeedFragment extends Fragment implements FeedView{

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    FeedPresenter presenter;

    static final String TAG = "FEED_FRAGMENT";

    /*
     * Some boilerplate fragment setup code
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_feed, container, false);
        ButterKnife.bind(this, view);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        setupToolar();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        menu.clear();
        inflater.inflate(R.menu.searchbar_menu, menu);

    }

    private void setupToolbar(){
        toolbar.setPadding(0,ScreenUtil.getStatusBarHeight(this.getActivity()),0,0);
        toolbar.setTitle("");
    }

    /*
     * Other logic code unique to each fragment
     */

      

Does anyone know if I can set both snippets for my toolbar options?

Thanks in advance.

+5


source to share


2 answers


When you call getActivity()

, it returns the same instance of your Activity

. The second fragment was initialized later Activity

than the first fragment, so it can only support the action bar in the second fragment.

You can try this



@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        AppCompatActivity baseActivity = (AppCompatActivity) getContext();
        baseActivity.setSupportActionBar(yourToolbar);
    }
}

      

+1


source


What worked for me was setting suppoerActionBar according to whichever fragment is currently visible. You can use the following code:



@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if(!hidden){
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    }
}

      

0


source







All Articles