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();
java
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.
add a comment |
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();
java
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.
add a comment |
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();
java
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
java
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.
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.
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password