How to keep a Textview text after the App closes

Asked

Viewed 169 times

1

Good, I’m new to Android, but I would like to know how I can keep/save the information that is in a Textview, so that this is not deleted when close application.

     public class Activity extends AppCompatActivity {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss");
        String strDate = sdf.format(c.getTime());

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

               private void EscreverData(){
                  TextView datas = (TextView) findViewById(R.id.datas);
                  datas.append(strDate);}}

Basically what I want is, when this activity starts, get the date and time and show this information in Textview. However, I would like her to continue storing the old dates and not erase them each time the activity is started again. I’ve tried searching a little and I think a good solution could be Sharedpreferences but I don’t know how to use it. Thanks if you can help me

  • Do you already have any code? It would be interesting if you put it here to help us identify the solution (https://answall.com/help/mcve).

  • Do a Sharedpreferences search.

  • @Here you go... if you can help me, I’d appreciate it

1 answer

3


A simple form of data persistence is to use the SharedPreference as shown in the documentation on How to save key value sets. Below is an example of how to save, considering you have any variable with any text:

String value = "Um texto qualquer";
SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("str_textview", value);
editor.commit();

To rescue:

SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
String text = pref.getString("str_textview", null);
meuTextView.setText(text );

See more details on this question about Save value in Sharedpreference.

This way, at any time of its application, it is possible to return the value that was saved in a certain key. See here Data Persistence Levels in Android Applications other approaches.

In the Kotlin would look like this:

val value = "Um texto qualquer"
val sharedpreferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE)
val editor = sharedpreferences.edit()
editor.putString("str_textview", value)
editor.commit()

To rescue:

val pref = context.getSharedPreferences("pref", MODE_PRIVATE)
val text = pref.getString("str_textview", null)
myTextView.setText(text)
  • I put the code in the question, can you help me put it all together? i just don’t know where to put the rest of the code you made available Thanks by the way for the help

  • @Eduardobrito you then wanted a list of dates, on which would be stored all the old dates? Or only the first date the user first entered?

  • Yes it is. A list with for example the last 5 dates

  • @Eduardobrito my answer was correct before editing. When you edited it, ended up invalidating it. I suggest creating another question further detailing your question, because an answer to your question has nothing to do with it here.

  • @Eduardobrito You could create a question with title: "How to store the user’s access date in a list?" ... there you describe your problem.

  • @Eduardobrito Because it doesn’t only involve Textview. It also involves a Listview. etc.

  • Maybe that’s exactly what I’m gonna do Thanks for the help :)

  • https://answall.com/questions/228729/howto store in a listas-5-%C3%baltimas-open-dates-of-an-activity-androi @acklay here

  • @Eduardobrito saw that you asked a question. I was going to try to put an answer there, but I’ve seen that you answered. If that answer here was helpful and if you want to validate it, feel free. Anything I’m around.

  • Thank you so much for all your help. I’m new to these things and I still feel some difficulties. Yes I will validate this one, because it helped a lot to solve the other

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.