package com.stevescodingblog.androidapps.tally;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    int _tally = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        final View layoutRoot = this.findViewById(R.id.layoutRoot);
        layoutRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _tally += 1;
                updateUi();
            }
        });

        final Button resetButton = (Button) this.findViewById(R.id.tallyButton);
        resetButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                _tally = 1;
                updateUi();
            }
        });
    }

    protected void updateUi() {

        final TextView tallyDisplay = (TextView)this.findViewById(R.id.tallyDisplay);
        tallyDisplay.setText(Integer.toString(this._tally));

    }
}

@@ -90,17 +90,84 @@
 oid.
-os.Bundle
+content.SharedPreferences;%0Aimport android.os.Bundle;%0Aimport android.util.Log
 ;%0Aim
@@ -251,16 +251,82 @@
 extView;
+%0Aimport android.widget.Toast;%0A%0Aimport java.util.prefs.Preferences;
 %0A%0Apublic
@@ -382,17 +382,17 @@
 tally = 
-1
+0
 ;%0A%0A    @
@@ -761,28 +761,24 @@
             
-    
 _tally += 1;
@@ -774,28 +774,24 @@
 tally += 1;%0A
-    
             
@@ -1052,28 +1052,24 @@
             
-    
 _tally = 1;%0A
@@ -1065,23 +1065,19 @@
 tally = 
-1
+0
 ;%0A
-    
         
@@ -1117,16 +1117,42 @@
      %7D);
+%0A%0A        this.updateUi();
 %0A    %7D%0A%0A
@@ -1331,16 +1331,804 @@
 ally));%0A
+    %7D%0A%0A    @Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        this.loadState();%0A        this.updateUi();%0A    %7D%0A%0A    @Override%0A    protected void onStop() %7B%0A        super.onStop();%0A        saveState();%0A    %7D%0A%0A    private void loadState() %7B%0A        // See if we have previously saved the count, and load it%0A        SharedPreferences prefs = getPreferences(MODE_PRIVATE);%0A        this._tally = prefs.getInt(%22count%22, 0);%0A    %7D%0A%0A    private void saveState() %7B%0A        // Get the preferences and the editor for the preferences%0A        SharedPreferences prefs = getPreferences(MODE_PRIVATE);%0A        SharedPreferences.Editor editor = prefs.edit();%0A%0A        // Store our count..%0A        editor.putInt(%22count%22, this._tally);%0A%0A        // Save changes%0A        editor.commit();
 %0A    %7D%0A%7D

Add state saving to Tally