C
C
Chesterfield252021-11-02 00:06:10
Java
Chesterfield25, 2021-11-02 00:06:10

How to save value from firebase database before exit?

I have several fragments that get the same information from the database, namely the user's balance, but when I close the application, the current balance is not saved to the application, how can I save the current balance, and update only if the balance has changed in the database itself?

Fragment

public class HomeFragment extends Fragment {


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


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

    }

    FragmentHomeBinding binding;

    //Firebase
    private FirebaseAuth mAuth;
    private FirebaseDatabase userDatabase;
    private FirebaseAuth.AuthStateListener listener;
    private FirebaseUser user;
    private DatabaseReference reference;
    private String firebaseId;


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

        binding = FragmentHomeBinding.inflate(inflater, container, false);

        userDatabase = FirebaseDatabase.getInstance();
        reference = userDatabase.getReference();
        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        firebaseId = user.getUid();


        stateListener();
        gettingDatabase();

        // Inflate the layout for this fragment
        return binding.getRoot();
    }

    private void stateListener(){
        listener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if(user == null){
                    Toast.makeText(getActivity(), "You Need To Login!", Toast.LENGTH_SHORT).show();
                    getActivity().finish();
                }
            }
        };
    }

    private void gettingDatabase(){
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                    User user = new User();
                    user.setPv(dataSnapshot.child(firebaseId).getValue(User.class).getPv());

                    binding.pv.setText(Integer.toString((int) user.getPv()));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getActivity(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(listener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if(listener != null) {
            mAuth.removeAuthStateListener(listener);
        }
    }
}


A section of code that is directly responsible for showing the balance

private void gettingDatabase(){
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                    User user = new User();
                    user.setPv(dataSnapshot.child(firebaseId).getValue(User.class).getPv());

                    binding.pv.setText(Integer.toString((int) user.getPv()));
                }
            }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question