Is it possible to save the markup with values ​​to a file in Android so that after that it can be downloaded from this file later?

Suppose I have main.xml, there are 2 Button and 20 TextView . For each TextView I wrote the text programmatically (each text is about 20 words).

Is it possible to somehow save it all and run it in a new Activity ? Just the same effect occurs when you rotate the screen, these two methods no longer work. ((

 onRetainNonConfigurationInstance() getLastNonConfigurationInstance() 

    1 answer 1

    If I understand correctly, you need to save the lines from the TextView somewhere and restore them after turning the activation or transfer to another activation. For this there is a class Bundle .

    In it, you can save your lines, and then extract.

    1. To save to the current activation, use the onSaveInstanceState(Bundle state) method:

       @Override public void onSaveInstanceState(Bundle state) { //находим TextView TextView tV=(TextView )this.findViewById(R.id.yours_text_view_id); String textFromTextViewToSave=tv.getText(); state.putString("str1", textFromTextViewToSave); } 
    2. To restore this text when you turn the onCreate in onCreate we write

       @Override public void onCreate(Bundle state) { //находим TextView TextView tV=(TextView )this.findViewById(R.id.yours_text_view_id); String textFromTextViewToSave=state.getString("str1"); tV.setText(textFromTextViewToSave); } 
    3. To transfer these values ​​to another activity, proceed by analogy, saving the lines from the TextView to the Bundle Intent , by which you launch this activity:

       Intent intent=new Intent(context, YourAnoterActivity.class); intent.putStringExtra("str1", textFromTextViewToSave); context.startActivity(intent); 

      , and restoring them to OnCreate of this activation as:

       @Override public void onCreate(Bundle state) { String fromIntent=this.getIntent.getString("str1"); } 
    4. You can also save to SharePreferences . So you will not depend on the life cycle.

       //сохраняем строку в файл внутренней директории приложения SharePreferences pref=PreferenceManager.getDefaultSharedPreferences(context); pref.edit().putString("key", "value").commit(); //получаем ранее сохранённые данные String savedData=pref.getString("key"); 
    • @Yuriy SPb, I need 3 ways. I just thought that when you destroy the activation, it automatically stores everything in the Bundle, so not to write String textFromTextViewToSave = state.getString ("str1"); 20 times, could it be ready, or is it empty? Bundle I just need to save the activation state that is, and after turning off the phone and turning it on, and running this program, so that it all reappears, by clicking download old activation. In short, do you know the Sleep mode on your computer?) Is the same. Is that so? - Futurama
    • one
      @Futurama still have to suffer. It is possible instead of 20 times once to save an array of strings. You can also override the TextView class and its setText (String text) method to write to the Bundle itself. But this is the path of a warrior. You can also write data to SharedPreferences and take them from there without using the Bundle. My advice is to put all your lines into an array or a list, assign TextView text from there and push it all into Bundle. - Yuriy SPb
    • 3
      I added an example with SharedPreferences to my answer. Try it. You do not need to write objects to the file, believe me. And just to do with this amount of data will not work. You must have a data model that you use in the adapter. You need to keep it. In any case there will be a lot of code. Even if you shove everything into a file with a string, you still have to write a parser to it. Do not reinvent the wheel. Use the 4th version of my answer or cut the Database in general) - Yuriy SPb
    • one
      @Futurama, you don't need this. Not exactly necessary. Use standard tools. - Yuriy SPb
    • one
      When you rotate the screen, you only need to save the data in the Bundle. Or a fragment special at the worst. No Preferences, because every turn will be writing / reading from the disk. and this will slow down work if there is a lot of data. - arg