0

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();
   realm.beginTransaction();
   final Email emailInput = new Email();
   emailInput.setUsername(editEmail.getText().toString());

   realm.copyToRealmOrUpdate(emailInput);
   realm.commitTransaction();


}


@OnClick(R.id.btn_forgot)
public void onButtonClick() {

   setEmail();

}

. 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(R.id.resend_user_email);
   if (resendEmailTxt != null) {
       this.resendEmailTxt.setText(resultEmail.getUsername());
   }


}
@Override
public void onStart() {
   super.onStart();
   getEmail();


}

Model class:

public class Email extends RealmObject {

@PrimaryKey
public int id = 0;
private String username;

//Getters and setters here

 }
  • How are the fragments set up? Are they contained in ViewPager, Activity etc? Are they created at the same time? Seems to me they are and you execute Fragment2's `getEmail()` before Fragment1's button is clicked. – Rafal Zawadzki Nov 16 '16 at 15:55
  • @RafałZawadzki They are in ViewPager, and I added them programmatically as List inside pagerAdapter.What can I do to execute button click before getEmail()? – Anonimo Anonimus Nov 16 '16 at 15:59
  • Fragments are inside TabFragment(ViewPager), and they are created at same time. @RafałZawadzki thx for helping bro – Anonimo Anonimus Nov 16 '16 at 16:09

1 Answers1

0

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.:

  • interface
  • event bus (Otto, EventBus)
  • reactive programming

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

Rafal Zawadzki
  • 963
  • 6
  • 15
  • Zawadzki thanks for helping me.I had same thoughts, but could not find solution for this. I am still junior dev, so do not really know how to inform second fragment that data has been changed.Can you please give me some more details about public method in Fragment2, or some other solution. – Anonimo Anonimus Nov 16 '16 at 16:25
  • This depends on your case, but I would recommend reading official Android documentation - that article exactly describes how to solve your problem using interface: https://developer.android.com/training/basics/fragments/communicating.html – Rafal Zawadzki Nov 16 '16 at 16:31
  • Zawadzki thanks, but I was already reading this.Problem is that every acitivity in my project is empty, and can not do things inside Activity.Is there any way to solve this only using Realm, or I have to figure out how to tell my second fragment that data has been changed – Anonimo Anonimus Nov 16 '16 at 16:38
  • If for whatever reason you can't use Activity, I would go with event bus (eg http://square.github.io/otto/). You define event and post it after Realm was updated. Second fragment is subscriber for this event and queries db as reaction to this event. This is really simple and you are totally capable of getting it to work :) – Rafal Zawadzki Nov 16 '16 at 16:50
  • I am already using RxJava, so do you think it is better solution?I can create Observable and subscribe to it.I was hoping that this would take less time and it was about fragment lifecycle. Thanks bro @Rafal Zawadzki – Anonimo Anonimus Nov 16 '16 at 17:44
  • Well you are right, it is about fragment lifecycle and you could fix it by making sure Fragment2 is not created before button on Fragment1 is clicked OR just querying when Fragment2 is visible (http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager) – Rafal Zawadzki Nov 16 '16 at 18:02