package com.example.a1.onsaveinstancestate; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main2Activity extends AppCompatActivity { Button button; String data = "+2point"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); button = (Button)findViewById(R.id.point); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Π—Π΄Π΅ΡΡŒ ΠΈΠ· ΠΎΠ΄Π½ΠΎΠ³ΠΎ Activity ΠΈΠ½ΠΈΡ†ΠΈΠΈΡ€ΡƒΡŽ измСнСния Π² Π΄Ρ€ΡƒΠ³ΠΎΠΌ Intent intent = new Intent(Main2Activity.this, Result.class); intent.putExtra("result", data.toString()); startActivity(intent); } }); } } 

Below Activity2, which gets the value of String, but for some reason does not save when restarting the application (main problem)

 package com.example.a1.onsaveinstancestate; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class Result extends AppCompatActivity { TextView getString; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); getString = (TextView)findViewById(R.id.textView); getString.setText(getIntent().getStringExtra("result")); } } 
  • one
    Problem in transferring data between an Activity or storing data when restarting an application? - Nikita Leshchev
  • 2
    first, why do you write a variable object of type Button to TextView? - Maxgmer
  • The problem is that it changes the textView, but then when I restart the application, the textView - Developer Chingis value is not saved
  • fixed, but still not working - Developer Chingis

2 answers 2

Wax the PreferenceManager , each time update the value as it changes. You can save the value as follows:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Main2Activity.this); SharedPreferences.Editor editor = prefs.edit(); editor.putString("data", data); editor.apply(); 

Get this:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Result.this); String newData = prefs.getString("data", ""); TextView getString = (TextView)findViewById(R.id.textView); getString.setText(newData); 
  • How does this get a textview? - Developer Chingis
  • In your case like this: getString.setText(newData); , instead of getString.setText(getIntent().getStringExtra("result")); - Nikita Leshchev
  • Sorry, another question. What is the lastData - Developer Chingis
  • I corrected my answer, so it should be clearer. - Nikita Leshchev
  • one
    Thank you so much !!! Everything is working - Developer Chingis

There are several ways to store data in Android:

Preferences - as a analogy, you can bring Windows INI files

SQLite - database, tables

regular files - internal and external (on the SD card)

If you need to save only one line between application launches, SharedPreferences (for an example of usage, see here ).