0

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?

1
  • if you want retrieve data from firebase, just create a reference object of your desired firebase path. Firebase ref=new Firebase(path); Commented Mar 8, 2016 at 8:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.