A
A
A2019-02-03 12:45:08
Java
A, 2019-02-03 12:45:08

How to store a value in another class in Android?

I have one class that stores the number entered by a person, after clicking the button it is transferred to another class, to the TextView, but after exiting this class, the number is not saved, how can I save it? With SharedPreferences or something else?

spoiler
public class Settings extends AppCompatActivity {

    private EditText one, two;
    private TextView result;
    private Button save;
    Integer perevod;

    int in;
    int in1;

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

        one = (EditText) findViewById(R.id.editTextOne);
        two = (EditText) findViewById(R.id.editTextTwo);
        save =(Button) findViewById(R.id.button);
        result = (TextView) findViewById(R.id.textView);


        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        int in2 = prefs.getInt("in",0);
        one.setText(""+ in2);

        int in3 = prefs.getInt("in1",0);
        two.setText(""+ in3);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number1 = Integer.parseInt(one.getText().toString());
                int number2 = Integer.parseInt(two.getText().toString());
                int sum = number1 / number2;
                result.setText(String.valueOf(sum));

                in = Integer.parseInt(one.getText().toString());
                in1 = Integer.parseInt(two.getText().toString());

                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Settings.this);
                SharedPreferences.Editor editor = prefs.edit();

                editor.putInt("in",in);
                editor.putInt("in1",in1);
                editor.apply();

                Intent i = new Intent(Settings.this,Menu.class);
                i.putExtra("in",in);
                startActivity(i);


            }

        });


    }
}

This is where there are 2 numbers, I move one number to another class and I need to save it there
. Here is another class:
public class Menu extends AppCompatActivity {

    TextView txtName;

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

        txtName = (TextView) findViewById(R.id.Name);

        int in = getIntent().getIntExtra("in",0);

        txtName.setText(String.valueOf(in));

    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ssurrokk, 2019-02-03
@ssurrokk

SharedPreferences yes, you can use it.
Your question is not very clear - after leaving the class, how is it?

M
MarkerToSummer, 2019-02-04
@MarkerToSummer

You can try to use the Bundle object, in which the contents of the intent will be placed when the activity is changed, and then get the values ​​\u200b\u200bfrom this object. There are methods for different data types, all take a key.

Bundle intentData= getIntent().getExtras();
int in = intentData.getInt("in");

Or the universal get() method, but for it you need to do type casting.
intentData.get("ключ").to<нужный тип>();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question