onDataChange() strange behavior. Functions called not working the first time











up vote
1
down vote

favorite












Application uses the onDataChange() method to check for changes within my Firebase Database and it works fine. I will update a textview depending on whether the child node exists in my database and it works fine.



However, Functions which I have written outside and called it together with the textview update will not run the very first time the activity is loaded. I have to move to a different activity then load the original activity again before the other functions will work as expected.



    PDatabase = FirebaseDatabase.getInstance().getReference().child("Prescription");

PDatabase.child(SignInIC.CustomUID).addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists()) {

String medname = dataSnapshot.child("medication").getValue().toString();
String Strength = dataSnapshot.child("dosage").getValue().toString();
String Method = dataSnapshot.child("method").getValue().toString();
String Pills = dataSnapshot.child("pills").getValue().toString();
String Freq = dataSnapshot.child("freq").getValue().toString();
String Duration = dataSnapshot.child("duration").getValue().toString();

Days = Duration + " day prescription of " + medname + "left";


if (Duration.equals("0"))
{

Days = "Your prescription of " + medname + "will end today";
}

Home_Days = "for " + Duration + " more days";

if (Duration.equals("0"))
{
Home_Days = "will end today";
}

if (Freq.equals("once a day "))
{
countDownTimerMedsAttndonce.start();
}

if (Freq.equals("twice a day "))
{
countDownTimerMedsAttndtwice.start();
}

if (Freq.equals("once a day every 12 hours "))
{
countDownTimerMedsAttndonce.start();
}

if (Freq.equals("twice a day every 12 hours "))
{
countDownTimerMedsAttndtwice.start();
}

if(medname.equals("Enoxaparin ") || medname.equals("Fondaparinux "))
{

TV.setVisibility(View.VISIBLE);
TV.setText(medname + "(" + Strength + ")" + Method + Freq + Home_Days);
addNotification();
countDownTimer.start();

}
else
{

TV.setVisibility(View.VISIBLE);
TV.setText(medname + Pills + "pills" + "(" + Strength + ")" + Method + Freq + Home_Days);
addNotification();
countDownTimer.start();
}
}
else
{
TV.setVisibility(View.VISIBLE);
TV.setText("You currently have no prescribed medication");

}

}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});

private void addNotification()
{
// Builds your notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Medimate")
.setContentText(Days)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT);

// Creates the intent needed to show the notification
if(findViewById(R.id.homeTV).isShown())
{
return;
}
else
{
Intent notificationIntent = new Intent(this, Alarm.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
}

// Add as notification
NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
nmagr.notify(1,notification);
}


The functions that wont execute the first time the activity is loaded are addNotification(); and countDownTimer.start();










share|improve this question









New contributor




Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    Application uses the onDataChange() method to check for changes within my Firebase Database and it works fine. I will update a textview depending on whether the child node exists in my database and it works fine.



    However, Functions which I have written outside and called it together with the textview update will not run the very first time the activity is loaded. I have to move to a different activity then load the original activity again before the other functions will work as expected.



        PDatabase = FirebaseDatabase.getInstance().getReference().child("Prescription");

    PDatabase.child(SignInIC.CustomUID).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    {
    if (dataSnapshot.exists()) {

    String medname = dataSnapshot.child("medication").getValue().toString();
    String Strength = dataSnapshot.child("dosage").getValue().toString();
    String Method = dataSnapshot.child("method").getValue().toString();
    String Pills = dataSnapshot.child("pills").getValue().toString();
    String Freq = dataSnapshot.child("freq").getValue().toString();
    String Duration = dataSnapshot.child("duration").getValue().toString();

    Days = Duration + " day prescription of " + medname + "left";


    if (Duration.equals("0"))
    {

    Days = "Your prescription of " + medname + "will end today";
    }

    Home_Days = "for " + Duration + " more days";

    if (Duration.equals("0"))
    {
    Home_Days = "will end today";
    }

    if (Freq.equals("once a day "))
    {
    countDownTimerMedsAttndonce.start();
    }

    if (Freq.equals("twice a day "))
    {
    countDownTimerMedsAttndtwice.start();
    }

    if (Freq.equals("once a day every 12 hours "))
    {
    countDownTimerMedsAttndonce.start();
    }

    if (Freq.equals("twice a day every 12 hours "))
    {
    countDownTimerMedsAttndtwice.start();
    }

    if(medname.equals("Enoxaparin ") || medname.equals("Fondaparinux "))
    {

    TV.setVisibility(View.VISIBLE);
    TV.setText(medname + "(" + Strength + ")" + Method + Freq + Home_Days);
    addNotification();
    countDownTimer.start();

    }
    else
    {

    TV.setVisibility(View.VISIBLE);
    TV.setText(medname + Pills + "pills" + "(" + Strength + ")" + Method + Freq + Home_Days);
    addNotification();
    countDownTimer.start();
    }
    }
    else
    {
    TV.setVisibility(View.VISIBLE);
    TV.setText("You currently have no prescribed medication");

    }

    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
    });

    private void addNotification()
    {
    // Builds your notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher_round)
    .setContentTitle("Medimate")
    .setContentText(Days)
    .setDefaults(Notification.DEFAULT_SOUND)
    .setAutoCancel(true)
    .setPriority(Notification.PRIORITY_DEFAULT);

    // Creates the intent needed to show the notification
    if(findViewById(R.id.homeTV).isShown())
    {
    return;
    }
    else
    {
    Intent notificationIntent = new Intent(this, Alarm.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    }

    // Add as notification
    NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification=builder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    nmagr.notify(1,notification);
    }


    The functions that wont execute the first time the activity is loaded are addNotification(); and countDownTimer.start();










    share|improve this question









    New contributor




    Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Application uses the onDataChange() method to check for changes within my Firebase Database and it works fine. I will update a textview depending on whether the child node exists in my database and it works fine.



      However, Functions which I have written outside and called it together with the textview update will not run the very first time the activity is loaded. I have to move to a different activity then load the original activity again before the other functions will work as expected.



          PDatabase = FirebaseDatabase.getInstance().getReference().child("Prescription");

      PDatabase.child(SignInIC.CustomUID).addValueEventListener(new ValueEventListener() {

      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
      {
      if (dataSnapshot.exists()) {

      String medname = dataSnapshot.child("medication").getValue().toString();
      String Strength = dataSnapshot.child("dosage").getValue().toString();
      String Method = dataSnapshot.child("method").getValue().toString();
      String Pills = dataSnapshot.child("pills").getValue().toString();
      String Freq = dataSnapshot.child("freq").getValue().toString();
      String Duration = dataSnapshot.child("duration").getValue().toString();

      Days = Duration + " day prescription of " + medname + "left";


      if (Duration.equals("0"))
      {

      Days = "Your prescription of " + medname + "will end today";
      }

      Home_Days = "for " + Duration + " more days";

      if (Duration.equals("0"))
      {
      Home_Days = "will end today";
      }

      if (Freq.equals("once a day "))
      {
      countDownTimerMedsAttndonce.start();
      }

      if (Freq.equals("twice a day "))
      {
      countDownTimerMedsAttndtwice.start();
      }

      if (Freq.equals("once a day every 12 hours "))
      {
      countDownTimerMedsAttndonce.start();
      }

      if (Freq.equals("twice a day every 12 hours "))
      {
      countDownTimerMedsAttndtwice.start();
      }

      if(medname.equals("Enoxaparin ") || medname.equals("Fondaparinux "))
      {

      TV.setVisibility(View.VISIBLE);
      TV.setText(medname + "(" + Strength + ")" + Method + Freq + Home_Days);
      addNotification();
      countDownTimer.start();

      }
      else
      {

      TV.setVisibility(View.VISIBLE);
      TV.setText(medname + Pills + "pills" + "(" + Strength + ")" + Method + Freq + Home_Days);
      addNotification();
      countDownTimer.start();
      }
      }
      else
      {
      TV.setVisibility(View.VISIBLE);
      TV.setText("You currently have no prescribed medication");

      }

      }
      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
      }
      });

      private void addNotification()
      {
      // Builds your notification
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
      .setSmallIcon(R.mipmap.ic_launcher_round)
      .setContentTitle("Medimate")
      .setContentText(Days)
      .setDefaults(Notification.DEFAULT_SOUND)
      .setAutoCancel(true)
      .setPriority(Notification.PRIORITY_DEFAULT);

      // Creates the intent needed to show the notification
      if(findViewById(R.id.homeTV).isShown())
      {
      return;
      }
      else
      {
      Intent notificationIntent = new Intent(this, Alarm.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      builder.setContentIntent(contentIntent);
      }

      // Add as notification
      NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Notification notification=builder.build();
      notification.flags = Notification.FLAG_AUTO_CANCEL;
      nmagr.notify(1,notification);
      }


      The functions that wont execute the first time the activity is loaded are addNotification(); and countDownTimer.start();










      share|improve this question









      New contributor




      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Application uses the onDataChange() method to check for changes within my Firebase Database and it works fine. I will update a textview depending on whether the child node exists in my database and it works fine.



      However, Functions which I have written outside and called it together with the textview update will not run the very first time the activity is loaded. I have to move to a different activity then load the original activity again before the other functions will work as expected.



          PDatabase = FirebaseDatabase.getInstance().getReference().child("Prescription");

      PDatabase.child(SignInIC.CustomUID).addValueEventListener(new ValueEventListener() {

      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
      {
      if (dataSnapshot.exists()) {

      String medname = dataSnapshot.child("medication").getValue().toString();
      String Strength = dataSnapshot.child("dosage").getValue().toString();
      String Method = dataSnapshot.child("method").getValue().toString();
      String Pills = dataSnapshot.child("pills").getValue().toString();
      String Freq = dataSnapshot.child("freq").getValue().toString();
      String Duration = dataSnapshot.child("duration").getValue().toString();

      Days = Duration + " day prescription of " + medname + "left";


      if (Duration.equals("0"))
      {

      Days = "Your prescription of " + medname + "will end today";
      }

      Home_Days = "for " + Duration + " more days";

      if (Duration.equals("0"))
      {
      Home_Days = "will end today";
      }

      if (Freq.equals("once a day "))
      {
      countDownTimerMedsAttndonce.start();
      }

      if (Freq.equals("twice a day "))
      {
      countDownTimerMedsAttndtwice.start();
      }

      if (Freq.equals("once a day every 12 hours "))
      {
      countDownTimerMedsAttndonce.start();
      }

      if (Freq.equals("twice a day every 12 hours "))
      {
      countDownTimerMedsAttndtwice.start();
      }

      if(medname.equals("Enoxaparin ") || medname.equals("Fondaparinux "))
      {

      TV.setVisibility(View.VISIBLE);
      TV.setText(medname + "(" + Strength + ")" + Method + Freq + Home_Days);
      addNotification();
      countDownTimer.start();

      }
      else
      {

      TV.setVisibility(View.VISIBLE);
      TV.setText(medname + Pills + "pills" + "(" + Strength + ")" + Method + Freq + Home_Days);
      addNotification();
      countDownTimer.start();
      }
      }
      else
      {
      TV.setVisibility(View.VISIBLE);
      TV.setText("You currently have no prescribed medication");

      }

      }
      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
      }
      });

      private void addNotification()
      {
      // Builds your notification
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
      .setSmallIcon(R.mipmap.ic_launcher_round)
      .setContentTitle("Medimate")
      .setContentText(Days)
      .setDefaults(Notification.DEFAULT_SOUND)
      .setAutoCancel(true)
      .setPriority(Notification.PRIORITY_DEFAULT);

      // Creates the intent needed to show the notification
      if(findViewById(R.id.homeTV).isShown())
      {
      return;
      }
      else
      {
      Intent notificationIntent = new Intent(this, Alarm.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      builder.setContentIntent(contentIntent);
      }

      // Add as notification
      NotificationManager nmagr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Notification notification=builder.build();
      notification.flags = Notification.FLAG_AUTO_CANCEL;
      nmagr.notify(1,notification);
      }


      The functions that wont execute the first time the activity is loaded are addNotification(); and countDownTimer.start();







      java android firebase






      share|improve this question









      New contributor




      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 15 hours ago









      Mikhail Kholodkov

      3,49342141




      3,49342141






      New contributor




      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 16 hours ago









      Joel Hong

      134




      134




      New contributor




      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Joel Hong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Joel Hong is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237617%2fondatachange-strange-behavior-functions-called-not-working-the-first-time%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Joel Hong is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Joel Hong is a new contributor. Be nice, and check out our Code of Conduct.













          Joel Hong is a new contributor. Be nice, and check out our Code of Conduct.












          Joel Hong is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237617%2fondatachange-strange-behavior-functions-called-not-working-the-first-time%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly