I have defined a global variable inside an activity, this variable changes depending on the button user has pressed.
If I go outside of my app UI and come back by holding the home button which shows the list of recent apps, then my variable keeps its state, but if I run my app again by pressing the app aicon the variable state is gone.
Is there a way to keep the state of the variable?
You can save the variable’s value during the
onSaveInstanceState()callback. Then you use the “icicle” parameter that is passed to andonCreate()to restore the value. You might be interested in reading Managing the Activity Lifecycle.Also note that Java has class variables and member variables but not global variables.