Good day. The application has two activities and a transition between them takes place in both directions. Both activities use the same activity_main.xml markup file. I create it like this:

First activity (MainActivity.java):

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Intent toOfflineMode = new Intent(MainActivity.this, OfflineActivity.class); ..// 

And the second activation (OfflineActivity.java):

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Intent toOnlineMode = new Intent(OfflineActivity.this, MainActivity.class); ..// 

And on the corresponding buttons I hang up startActivity(toOfflineMode); and startActivity(toOnlineMode); respectively. So it turns out that if you go to the second, and then back to the first, the data that was displayed in the first is lost. One gets the feeling that every time a button is pressed, a new copy of the activation is constantly created (on the screen, a new window runs from right to left). If you press the "back" button from the second activation, the first one is returned (from left to right on the screen) and the data on it all remain as it should be. Please tell me how to make the transition between these two activities on the button without losing data in both? Thank.

    2 answers 2

    Since the markup is the same for both activites, it is easier to do everything in one activit. And for data, create a separate class, and store data in objects of this class. By the button, the data is already loaded into the markup from the corresponding object.

    • One activity is a special case. Next are planned new activations with different markup ... - Pollux
    • 2
      You can try to use fragments. In Aktviti store all the data, and load various fragments. You can also create a singleton class and store the data of each activation in it. - Frozik6k
    • I read about the singleton, it seemed to be even realized. Very interesting thing! But still it is more such a permanent storage of variables. I would like to fully maintain the state of activation. If you go to the second activation, and then return to the first via onBackPressed() full state of the first is saved somehow. For example, there is a listview , you can save data from it, but, say, the scroll position will not be saved. In addition, if the listview downloads data from the Internet, it is also a problem. - Pollux
    • This is at the OS level in the stack is saved activation. Only here as you complete the last activation, it is destroyed. Maybe if only manually using SaveInstanceState , because if you exit the activation using the finish() or onBackPressed , then when you start the onRestoreInstanceState again, the onRestoreInstanceState method onRestoreInstanceState not start. - Frozik6k

    You can use the onSaveInstanceState () method.