Anonimo Anonimus Anonimo Anonimus - 1 year ago 106
Android Question

Realm is using old object, not updating it

I am using Realm for saving user input from EditText in Fragment 1 , and showing that input inside TextView of Fragment 2.When user clicks button inside first Fragment , I need to show that text inside second Fragment.

Everything seems fine, but every time I lunch my app, query is excetuded before saving object and old input is shown inside TextView.

  • When I enter some input, old text has been saved already and shown
    inside TextView

Here is method for saving input, and OnClick Method inside Fragment 1 :

public void setEmail() {
Realm realm = getRealm();
final Email emailInput = new Email();



public void onButtonClick() {



Here is method for query and setting text inside Fragment 2 :

public void getEmail() {
Realm realm = getRealm();
RealmQuery<Email> queryUser = realm.where(Email.class);
Email resultEmail = queryUser.findFirst();

resendEmailTxt = (AutoResizeTextView) getView().findViewById(;
if (resendEmailTxt != null) {

public void onStart() {


Model class:

public class Email extends RealmObject {

public int id = 0;
private String username;

//Getters and setters here


Answer Source

Second Fragment queries data when it starts, that is before onClick event from first Fragment can happen (because both fragments get created roughly at the same time).

You need to inform second fragment that the data has been changed in the first one. There are many ways of accomplishing that, eg.:

  • public method in Fragment2
  • interface
  • event bus (Otto, EventBus)
  • reactive programming

There is probably a lot more but one of these will be enough ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download