Hide Actionbar in one Ragment but show in another

Asked

Viewed 258 times

2

I am working on a project that is being done virtually all based on fragments. So far, I’ve only got one activity and 4 fragments, within which I need only one of them not to have the ActionBar. Common methods of hiding from Actionbar have not worked (getActivity().getSupportActionBar().hide();). How can I do that?

This is the Fragment that I do not wish to have Actionbar:

package com.renanlazarotto.fserv.fragments;

        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.Button;

        import com.renanlazarotto.fserv.activities.FservActivity;
        import com.renanlazarotto.fserv.R;

public class LoginFragment extends Fragment {
    public LoginFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ((FservActivity) getActivity()).getSupportActionBar().setTitle(R.string.titulo_login);

        View view = inflater.inflate(R.layout.fragment_login, container, false);

        Button logar = (Button) view.findViewById(R.id.login_button_entrar);

        logar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, new ChamadosFragment()).commit();
            }
        });

        return view;
    }
}

1 answer

2


After a little research, I realized that apparently it is not possible to hide the ActionBar in just one Fragment without affecting others. Thus, I solved the problem as follows:

public static void mostrarActionBar(Activity parent) {
    ActionBarActivity abc = (ActionBarActivity) parent;
    abc.getSupportActionBar().show();
}
public static void esconderActionBar(Activity parent) {
    ActionBarActivity abc = (ActionBarActivity) parent;
    abc.getSupportActionBar().hide();
}

Where the Parent parameter is passed as a reference from which Activity the Fragment belongs (using the method getActivity()):

esconderActionBar(getActivity());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.