Abhi
Abhi

Reputation: 3511

Persistent Firebase Database

I am making a voting app. There are two buttons and the number of clicks are stored in a Firebase database. However on closing the app(kill app process), the database gets refreshed and the counter starts from zero again. Is it possible for the database to start counting from where it left even after the app is killed.

Source Code for MainActivity.java

public class Main2Activity extends AppCompatActivity {
private Firebase mRootRef;
private Button mBtn1;
private Button mBtn2;
int counter = 0;
int counter1 = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Firebase.setAndroidContext(this);
    mBtn1 = (Button) findViewById(R.id.btn1);
    mBtn2 = (Button) findViewById(R.id.btn2);
    mBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.equals(mBtn1)) {
                mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
                Firebase mRefChild = mRootRef.child("Votes");
                counter++;
                mRefChild.setValue(counter);
                MediaPlayer click1 =MediaPlayer.create(getApplicationContext(), R.raw.click);
                click1.start();
                mBtn1.setEnabled(false);
                mBtn2.setEnabled(false);
                Timer buttonTimer = new Timer();
                buttonTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mBtn1.setEnabled(true);
                                mBtn2.setEnabled(true);
                            }
                        });
                    }
                }, 5000);
                final AlertDialog.Builder Voted = new AlertDialog.Builder(Main2Activity.this);
                Voted.setTitle("Voted");
                Voted.setMessage("You have cast Your Vote!");
                Voted.setCancelable(false);
                final AlertDialog dlg = Voted.create();
                dlg.show();

                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        dlg.dismiss();
                        t.cancel();
                    }
                }, 5000);
            }

        }
    });


    mBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
            if (v.equals(mBtn2)) {
                Firebase mRefChild = mRootRef.child("Votes");
                counter1++;
                mRefChild.setValue(counter1);
                MediaPlayer click2 =MediaPlayer.create(getApplicationContext(), R.raw.click);
                click2.start();
                mBtn2.setEnabled(false);
                mBtn1.setEnabled(false);
                Timer buttonTimer = new Timer();
                buttonTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mBtn2.setEnabled(true);
                                mBtn1.setEnabled(true);
                            }
                        });
                    }
                }, 5000);
                final AlertDialog.Builder Voted = new AlertDialog.Builder(Main2Activity.this);
                Voted.setTitle("Voted");
                Voted.setMessage("You Have cast your Vote");
                Voted.setCancelable(false);
                final AlertDialog dlg = Voted.create();
                dlg.show();

                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        dlg.dismiss();
                        t.cancel();
                    }
                }, 5000);

            }
            }
        });
    }
@Override
public void onBackPressed() { }

}

Thanks

Upvotes: 0

Views: 432

Answers (1)

Rahul Patel
Rahul Patel

Reputation: 617

Counter and counter1 are the local variable and you are not persisting them. As you are storing the value of counter and counter1 to the database, you should simply retrieve the value and increment it by 1.

mRefChild.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // you can retrieve your stored value from dataSnapshot 
            Object data = dataSnapshot.getValue()
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

It sounds like you are using order version of Firebase. Please refer this link https://firebase.google.com/support/guides/firebase-android and upgrade your project.

Above link also provides details about reading the data. In your case addListenerForSingleValueEvent(..) will solve your problem.

https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html

from your code it sounds like you directly storing value for Votes and there is no table structure or anything. So dataSnapshot.getValue() should return your value. however, I advise you to debug the code and and put a breakpoint Object data = dataSnapshot.getValue() and analyze the information in the dataSnapshot instance. Its all there.

once you retrieve the value, assign it to the counter or counter1 variable. so everytime you will start app, you will have updated value of votes back to your variable. you are already doing counter++ on button click so that should work as it is.

Upvotes: 1

Related Questions