简体   繁体   中英

How do I save TextViews when closing an App?

I'm currently creating an app which includes creating TextViews, every time a button is clicked. However, when I close the app and return to it, all the TextViews which were previously created with the button click are not there anymore. I tried searching for a solution, however, I found nothing, can anyone help me?

Every help is much appreciated.

When you are closing the app save the value in shared preference

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.putString("txtvalue_key", "value");
     editor.apply();

Retrieve data from preference:

     SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    String textValue = prefs.getString("txtvalue_key", "");//"" is the default value.

After resuming your app or Restart your app

check there is any value saved in shared preference or not, if you found that there is a value saved, now set the value in the textview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM