I am creating an app in which points increases on button click and those points should be saved to firebase. I managed to save those data to firebase. But when I destroy my app and open it again the points value is showing same but after clicking button. It again starts from 0.
For example: every time on button click the points value increases to 10 points. Now when I completely destroy the app and open it again, the points value shows same, but when button clicked it again starts from initial condition.
Here is my code
int amount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amount = amount + 100;
textView_points.setText(String.valueOf(amount));
databaseReference.setValue(textView_points.getText().toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() == null) {
finish();
Intent main = new Intent(this,MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
}
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
textView_points.setText(dataSnapshot.getValue(String.class));
databaseReference.setValue(textView_points.getText().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Main2Activity.this,\"error\",Toast.LENGTH_LONG).show();
}
});
}
Before destroying the app see the image please, points increasing on button click before destroying the app and uploading to database please see image 1
Opened app again(after closing) it is showing the same updated point please see image 2
Now when i click claim it returns back to 100 please see image number 3
please help me on this problem, and i am a newbie Thanks