Home > Blockchain >  How can i show toolbar back button in fragments in android?
How can i show toolbar back button in fragments in android?

Time:08-08

I am working on an android application where I have three fragments I want to show the back button on the toolbar of the fragments to go back to the previous fragments now how can I achieve that?? I am new to android please guide me.

public class SecondFragment extends Fragment {



Button button2;
private PageViewModel viewModel;
EditText editTextName, editTextEmail, editTextDesc;
public SecondFragment() {
    // Required empty public constructor
}



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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_second, container, false);
    button2 = view.findViewById(R.id.next2);
    editTextName = view.findViewById(R.id.edittextName);
    editTextEmail =  view.findViewById(R.id.edditextEmail);
    editTextDesc = view.findViewById(R.id.edittexDesc);
    viewModel = new ViewModelProvider(requireActivity()).get(PageViewModel.class);
    


    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (editTextName.getText().toString().isEmpty() || editTextEmail.getText().toString().isEmpty() || editTextDesc.getText().toString().isEmpty())
            {

                editTextName.setError("Enter name");
                editTextEmail.setError("Enter Email");
                editTextDesc.setError("Enter Description");

            }else {

             //   viewModel.setName(editTextName.getText().toString());
                enterName(editTextName.getText().toString());
                enterEmail(editTextEmail.getText().toString());
                enterDesc(editTextDesc.getText().toString());
              //  viewModel.setEmail(editTextEmail.getText().toString());
              //  viewModel.setDescription(editTextDesc.getText().toString());
                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                Fragment NAME = new LastFragment();
                fragmentTransaction.replace(R.id.container, NAME);
                fragmentTransaction.commit();
            }
        }
    });

    return view;
}

this is the code of my fragment borther i have three fragments like this i need to add the back button on the toolbar of there three fragemtns

CodePudding user response:

You can add a toolbar to your activity, which is the container for these fragments. Then in your fragments you can do something like this:

private void setupToolbar() {
   YourActivity yourActivity = (YourActivity) getActivity()
   if(yourActivity != null) {
      yourActivity.toolbar.setSkipVisibility(View.GONE) // Or View.VISIBLE
      yourActivity.toolbar.setText() // Set some text
      yourActivity.toolbar.setOnBackButtonClickListener{ yourActivity.onBackPressed() }
   }
}

CodePudding user response:

Follow the steps down below.

fragmentTransaction.replace(R.id.container, NAME);
fragmentTransaction.addToBackStack(null);  //Add this line
fragmentTransaction.commit();

This will solve your issue. Thank you.

  • Related