-2

I'm having trouble understanding how sharedpreferences work so I need your help.

How do I store the data of the counter so that when the user kills/pauses/goes to a new activity in the app then comes back to this activity, the counter will still be in the same state when they left it?

int n1 = Integer.parseInt(num1.getText().toString());
                        int n2 = Integer.parseInt(num2.getText().toString());
                        counter =counter+(n1*n2);
                        TextView totalemission;
 totalemission=(TextView)findViewById(R.id.total);
                        totalemission.setText(String.valueOf(counter));

Also, I want the user to be able to continue to add (n1*n2) to the counter when they come back to the application.

This is the rest of the code:

    package com.carschoolingpromo.scires.carschooling;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class Calculator extends AppCompatActivity {

    public static final String MY_PREFS_NAME ="MyPrefsFile";
    int counter =0;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        Button caremissionscheck = findViewById(R.id.checkcar);
        caremissionscheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent tocarbonemissions = new Intent(Calculator.this, CarbonEmissions.class);
                startActivity(tocarbonemissions);
            }
        });

        final Button distancecheck = findViewById(R.id.checkdestination);
        distancecheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent checkdistance = new Intent(Calculator.this, DistancesList.class);
                startActivity(checkdistance);
            }
        });

        final Button multiply =(Button)findViewById(R.id.multiplycounter);
        final EditText num1 =(EditText)findViewById(R.id.carboninput);
        final EditText num2 =(EditText)findViewById(R.id.distanceinput);
        final TextView ans =(TextView)findViewById(R.id.answer);
        final Button gotonext=(Button)findViewById(R.id.nextsequence);

        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int n1 = Integer.parseInt(num1.getText().toString());
                int n2 = Integer.parseInt(num2.getText().toString());

                ans.setText(String.valueOf(n1*n2));

                Button gettotal;
                gettotal=(Button)findViewById(R.id.carpooltoday);
                gettotal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String number1 = num1.getText().toString();
                        String number2 = num2.getText().toString();
                        final int n1 = Integer.parseInt(num1.getText().toString());
                        final int n2 = Integer.parseInt(num2.getText().toString());
                        counter =counter+(n1*n2);
                        final TextView totalemission;

                        totalemission=(TextView)findViewById(R.id.total);
                        totalemission.setText(String.valueOf(counter));

                        final String string = totalemission.getText().toString();

                        Button save, load;

                        save =(Button)findViewById(R.id.save);
                        save.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                onResume();
                                onStart();
                                onStop();

                                sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("number1", num1.getText().toString());
                                editor.putString("number2", num2.getText().toString());
                                editor.putString("emission",totalemission.getText().toString());
                                editor.apply();
                                Toast.makeText(Calculator.this, "Saved!", Toast.LENGTH_SHORT).show();

                                }

                            });

                        load=(Button)findViewById(R.id.load);
                        load.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                                String firstnum = sharedPreferences.getString("number1", "");
                                String secondnum = sharedPreferences.getString("number2", "");
                                String thirdnum = sharedPreferences.getString("emission", "");

                                TextView carcarbon, distance, totalexp;

                                carcarbon=(TextView)findViewById(R.id.car1);
                                carcarbon.setText(firstnum);

                                distance=(TextView)findViewById(R.id.distance1);
                                distance.setText(secondnum);

                                totalexp=(TextView)findViewById(R.id.previoustotal);
                                totalexp.setText(thirdnum);









                    }
                });

                final String carbonfootprint = ans.getText().toString();

                gotonext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent k = new Intent(Calculator.this, NextStep.class);
                        k.putExtra("carbonfootprint", carbonfootprint);
                        startActivity(k);
                    }
                });


            }
        });


            }



    });}}
sigh
  • 27
  • 4

1 Answers1

1

I saw you just import SharedPreferences, please do some research on Android Shared preferences example and https://developer.android.com/training/basics/data-storage/shared-preferences.html

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putInt("n1", Integer.parseInt(num1.getText().toString()));
 editor.putInt("n2", Integer.parseInt(num2.getText().toString()));
 editor.apply();

when user come back to the application, retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
  int n1 = prefs.getInt("n1", 0);// 0 is the default value.
  int n2 = prefs.getInt("n2", 0); //0 is the default value.
Paul Chu
  • 1,209
  • 3
  • 16
  • 23