Update butonu ile değilde Fragment'den çıkarken yapılan değişiklikleri update yapmak istiyorm bunu nasıl yaparım->package com.example.newflashcard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.util.HashMap;
import java.util.Map;

public class fragment_edit extends Fragment {

    private EditText editTextCategory, editTextTitle, editTextContent;
    private CheckBox checkBoxFavorite, checkBoxPinned;
    private Button buttonUpdateFlashcard;

    private String flashcardId;

    public fragment_edit() {
        // Required empty public constructor
    }

    public static fragment_edit newInstance(String param1, String param2) {
        fragment_edit fragment = new fragment_edit();
        Bundle args = new Bundle();
        args.putString("param1", param1);
        args.putString("param2", param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            flashcardId = getArguments().getString("id");
            String category = getArguments().getString("category");
            String title = getArguments().getString("title");
            String content = getArguments().getString("content");
            boolean favorite = getArguments().getBoolean("favorite");
            boolean pinned = getArguments().getBoolean("pinned");
            String backgroundColor = getArguments().getString("backgroundColor");
            long timestamp = getArguments().getLong("timestamp");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit, container, false);

        editTextCategory = view.findViewById(R.id.editTextCategory);
        editTextTitle = view.findViewById(R.id.editTextTitle);
        editTextContent = view.findViewById(R.id.editTextContent);
        checkBoxFavorite = view.findViewById(R.id.checkBoxFavorite);
        checkBoxPinned = view.findViewById(R.id.checkBoxPinned);
        buttonUpdateFlashcard = view.findViewById(R.id.buttonUpdateFlashcard);

        // Bundle'dan gelen bilgileri EditText ve CheckBox'lara yükle
        if (getArguments() != null) {
            editTextCategory.setText(getArguments().getString("category"));
            editTextTitle.setText(getArguments().getString("title"));
            editTextContent.setText(getArguments().getString("content"));
            checkBoxFavorite.setChecked(getArguments().getBoolean("favorite"));
            checkBoxPinned.setChecked(getArguments().getBoolean("pinned"));
        }

//        buttonUpdateFlashcard.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String category = editTextCategory.getText().toString();
//                String title = editTextTitle.getText().toString();
//                String content = editTextContent.getText().toString();
//                boolean favorite = checkBoxFavorite.isChecked();
//                boolean pinned = checkBoxPinned.isChecked();
//
//                if (!category.isEmpty() && !title.isEmpty() && !content.isEmpty()) {
//                    updateFlashcard(flashcardId, category, title, content, favorite, pinned);
//                    Toast.makeText(getActivity(), "Flashcard updated", Toast.LENGTH_SHORT).show();
//                    getActivity().getSupportFragmentManager().popBackStack();
//                } else {
//                    Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((MainActivity) requireActivity()).hideToolbar();
        ((MainActivity) requireActivity()).hideFab();
    }

    @Override
    public void onPause() {
        super.onPause();
        ((MainActivity) requireActivity()).showToolbar();
        ((MainActivity) requireActivity()).showFab();
    }
    @Override
    public void onDetach() {
        super.onDetach();

        buttonUpdateFlashcard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String category = editTextCategory.getText().toString();
                String title = editTextTitle.getText().toString();
                String content = editTextContent.getText().toString();
                boolean favorite = checkBoxFavorite.isChecked();
                boolean pinned = checkBoxPinned.isChecked();

                if (!category.isEmpty() && !title.isEmpty() && !content.isEmpty()) {
                    updateFlashcard(flashcardId, category, title, content, favorite, pinned);
                    Toast.makeText(getActivity(), "Flashcard updated", Toast.LENGTH_SHORT).show();
                    getActivity().getSupportFragmentManager().popBackStack();
                } else {
                    Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    // Flashcard'ı güncellemek için genel bir metod
    private void updateFlashcard(String id, String category, String title, String content, boolean favorite, boolean pinned) {
        Map<String, Object> updates = new HashMap<>();
        updates.put("category", category);
        updates.put("title", title);
        updates.put("content", content);
        updates.put("favorite", favorite);
        updates.put("pinned", pinned);
        FirebaseManager.getInstance().updateFlashcard(id, updates);
    }
}