Asif
Asif

Reputation: 69

App title doesn't change after returning to previous fragment

I've been developing an android app which I included the default Navigation-Drawer from Android Studio and so on. In my home fragment, I've implemented CarViews, and then set those cardview(s) OnClickListener to replace the fragment with traditional procedure. After the fragment replacement and new page comes, I wanted to change the Actionbar title. So in the onCreateView(...) method, I tried,

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");

It worked. But after pressing the hardware back button to go back to the stacked fragment, the title remains changed & it doesn't change to "Home" again. I've tried other ways. Here's my following codes. Thanks in advance.

public class HomeFragment extends Fragment implements View.OnClickListener {
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).setTitle("Home");

        CardView cardView1 = root.findViewById(R.id.doctor_on);
        CardView cardView2 = root.findViewById(R.id.ambulance_e);
        CardView cardView3 = root.findViewById(R.id.maintainance_s);

        cardView1.setOnClickListener(this);
        cardView2.setOnClickListener(this);
        cardView3.setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.doctor_on:
                FragmentTransaction fragmentTransaction = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
                Fragment fragment1 = new doctors();
                fragmentTransaction.replace(R.id.container1, fragment1).addToBackStack(getString(R.string.menu_home)).commit();
                return;
            case R.id.ambulance_e:
                //Put Actions
                FragmentTransaction fragmentTransaction2 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
                Fragment fragment2 = new ambulance();
                fragmentTransaction2.replace(R.id.container1, fragment2).addToBackStack(getString(R.string.menu_home)).commit();
                return;
            case R.id.maintainance_s:
                //Put Actions
                FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
                Fragment fragment3 = new maintanance();
                fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
                return;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
    }
}

To the next fragment(where I change the titlebar and pressed back button):

public class doctors extend Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("B");
        View root = inflater.inflate(R.layout.fragment_doctors, container, false);

        return root;
    }

}

Upvotes: 1

Views: 747

Answers (3)

Asif
Asif

Reputation: 69

And in the doctors Fragment... Should fix the title error.

@Override
public void onDestroyView() {
    super.onDestroyView();
    Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity()))
            .getSupportActionBar())
            .setTitle(getString(R.string.your_title_here));
}

Upvotes: 1

Jaimil Patel
Jaimil Patel

Reputation: 1347

Add below code in Home Fragment...

@Override
    public void onHiddenChanged(Boolean hidden) {
        super.onHiddenChanged(hidden);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Home");
    }

Upvotes: 0

Hector Hugo Alonzo
Hector Hugo Alonzo

Reputation: 1

You have to override the method onBackPressed() and write the code:

@Override
public View onBackPressed(){
    //Here goes the code that head back to your main fragment
    FragmentTransaction fragmentTransaction3 = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
            Fragment fragment3 = new maintanance();
            fragmentTransaction3.replace(R.id.container1, fragment3).addToBackStack(getString(R.string.menu_home)).commit();
}

}`

Upvotes: 0

Related Questions