There are applications with multiple screens and activities. How can you make it so that when opening activit2 it does not stop the activity of activit1, or to save the state of activit1, to suspend it when activit2 is started, and then from activit2 to switch back to activit1.
1 answer
With the default settings, the second activation and so opens on top of the first, because if you press the back button from the second activation, the first appears (this is done using activity back stack ).
To save the activation state, you need to override its onSaveInstanceState(...) and (optionally) onRestoreInstanceState(...) methods.
In the onSaveInstanceState(...) method you save the onSaveInstanceState(...) state to an object of the Bundle type, in the onRestoreInstanceState(...) method you restore the onRestoreInstanceState(...) state from an object of the Bundle type.
An object of the Bundle type formed in the onSaveInstanceState(...) method is also passed to the onCreate(...) method, so you can restore the onCreate(...) state directly in the onCreate(...) method, and not in onRestoreInstanceState(...) .
- Activities are switched between each other using intent. Tell me, is it possible, having caused the activity to return from it to the past without intent? As for state saving, as I understand it, you need to save the bundle object in a variable when it is paused, and when you return from there, take it and insert it into onRestoreInstanceState? - Bullshit
- Yes, you can, just press the
backbutton. You need to save the state to an object of typeBundlein theonSaveInstanceState(...)method. - post_zeew