Kartik Ohri
Kartik Ohri

Reputation: 325

Retrieving data from firebase database in android

I'm recently migrated to Firebase. I have gone through Firebase Android api docs for retrieving data from database through JSON. there is method onDataChange(DataSnapshot snapshot) to retrieve data whenever there is change in data in database but I could find how to retrieve data even if there is no change in database.

For example:- when a user logs into my app, I want that data which is stored under the unique id node should be retrieved. How can I retrieve data anytime from Firebase database if i want?

Upvotes: 0

Views: 3594

Answers (2)

viral 9966
viral 9966

Reputation: 525

Firebase Read and Write Database Guide lines: https://firebase.google.com/docs/database/android/read-and-write

public class NavigationActivity extends AppCompatActivity{
private DatabaseReference mDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
 @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }


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

initializeAuthListener();

 mDatabase = FirebaseDatabase.getInstance().getReference();



}

private void initializeAuthListener() {
        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                firebaseUser = firebaseAuth.getInstance().getCurrentUser();
                try {
                    if (firebaseAuth != null) {
                        loadUserDetails();
                        Log.d("@@@@", "thread:signed_in:" + firebaseUser.getUid());
                    } else {
                        Log.d("@@@@", "thread:signed_out");
                        Intent login = new Intent(NavigationActivity.this, LoginActivity.class);
                        startActivity(login);
                        finish();
                    }
                }catch (Exception e){
                    Intent login = new Intent(NavigationActivity.this, LoginActivity.class);
                    startActivity(login);
                    finish();
                }

            }
        };
        mAuth.addAuthStateListener(mAuthListener);
    }

private void loadUserDetails() {
        DatabaseReference userReference = mDatabase
                .child("users").child(firebaseUser.getUid());

//        displayUserDetails(userReference);
        userReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                user = dataSnapshot.getValue(User.class);

                tv_user_name.setText(user.getDisplayName());
                tv_user_email_nav.setText(user.getEmail());

                Glide.with(NavigationActivity.this)
                        .load(user.getPhotoUrl())
                        .placeholder(R.mipmap.profile)
                        .centerCrop()
                        .dontAnimate()
                        .bitmapTransform(new CropCircleTransformation(NavigationActivity.this))
                        .into(iv_user_image);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
//                Toast.makeText(ThreadActivity.this, R.string.error_loading_user, Toast.LENGTH_SHORT).show();
//                finish();
            }
        });
    }
}

https://stackoverflow.com/a/45328201/5973946

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

From the Firebase guide on reading data:

[the onDataChange() method is] triggered once with the initial data and again every time the data changes.

I highly recommend that you read the Firebase guide for Android programming end-to-end. It'll answer many of the questions you're likely to have as you start using Firebase.

Upvotes: 2

Related Questions