1

Okay so i am making a very basic app. (Its not a chat app) But i want to do something that chat apps do. Plus i will be the only user for this app.

So what i want is, I am using firebase, so i want to create a listener that never expires, it reads for new data (or changes) even when app is closed and removed from memory, so even if i close it from recents, it should keep listening and whenever some changes occur, a function should run.

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Femn Dharamshi
  • 527
  • 1
  • 9
  • 25
  • Hello @DougStevenson I saw you added the firebase-database tag. Can you perhaps look at [my proposal](https://meta.stackoverflow.com/q/373283/4916627) to rename that tag? (Feel free to flag this comment as no longer needed after you read it) – André Kool Aug 28 '18 at 18:09

1 Answers1

0

What you're trying to do isn't possible. Your app can only listen to updates while it's running. When your app process is killed by Android when the user is no longer actively using it, you will have to re-add any listeners that you previously added, if you want to continue to receive updates.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441