Faced such a problem that the application does not save the activation state when the screen orientation changes.

Googling this info, I added a couple of methods, as it was said, but nothing happens. I ask for help in solving this problem.

Here is the code:

public class ReaderActivity extends AppCompatActivity { TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_reader); text = (TextView) findViewById(R.id.tvMainText); try { openBook(getIntent().getStringExtra("PATH_TO_BOOK")); } catch (IOException exc){ System.out.println("IO Error!"); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } } 
  • one
    You have added methods for saving / restoring state, but you do not save / restore anything in them. What list state do you want to save? - post_zeew
  • ListView. I need to save all the elements that are displayed at the moment and then display them in the same order. I have an idea to save the necessary parameters and just redraw the list later, but it seems to me that there is a less resource-intensive method. - Nick
  • one
    No, everything is done that way. In the Bundle save the list with the displayed data and, optionally, the state of the scroll. Further from the same Bundle you restore all this. - post_zeew
  • To answer your question, the structure of the data displayed in the list is missing. - post_zeew
  • Suppose there is an array with the data to display. - Nick

2 answers 2

Don't bother with onStateInstanceState / onRestoreInstanceState. It is better to immediately take a framework that supports the view state out of the box, and forget about this problem forever.

enter image description here

https://github.com/Arello-Mobile/Moxy

Watch here: https://www.youtube.com/watch?v=KZ0j2K9VAf8

Read here: https://habrahabr.ru/post/276189/

    Next will be presented the basic method of maintaining the state of objects, which is provided out of the box . You can also use other tools to solve such problems, but only after such fundamental things have been studied.


    Suppose you need to save the state of ArrayList<String> mStrings during the ArrayList<String> mStrings life cycle.

    In the simplest case, you can use the onSaveInstanceState(...) method and, optionally, the onRestoreInstanceState(...) method.

     public class MainActivity extends AppCompatActivity { private final String LIST_KEY = "LIST_KEY"; private ArrayList<String> mStrings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { mStrings = new ArrayList<>(); mStrings.add("First line"); mStrings.add("Second line"); mStrings.add("Third line"); } else { mStrings = savedInstanceState.getStringArrayList(LIST_KEY); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putStringArrayList(LIST_KEY, mStrings); } } 

    When the application starts, ArrayList<String> mStrings and populated with three lines.

    If you rotate the screen in particular and when you change the configuration of the device as a whole, before calling the onStop() method, the onSaveInstanceState(...) method will be called in which you can save data to an object of type Bundle .

    Further, these saved data can be restored in the methods: onCreate(...) and onRestoreInstanceState(...) .

    Thus, here in the onSaveInstanceState(...) method we put the list into the outState object, and then, in the onCreate(...) method, onCreate(...) restore our list.