By default, when the screen is rotated, that is, the application changes from portrait
to landscape
or vice versa, the Activity is killed and recreated. To ensure that no information is lost, for example if you reset the variable xCuenta , you have to save and re-establish the data using the life cycle methods.
You have more information at developer.android.
I'll give you a small sample code from that website and I'll give you a quick explanation.
public class CalendarActivity extends Activity {
...
static final int DAY_VIEW_MODE = 0;
static final int WEEK_VIEW_MODE = 1;
private SharedPreferences mPrefs;
private int mCurViewMode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//2
SharedPreferences mPrefs = getSharedPreferences();
mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
}
protected void onPause() {
super.onPause();
//1
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("view_mode", mCurViewMode);
ed.commit();
}
}
In 1: The onPause method is called before rotating the screen, we take advantage of that moment to store any variable that we need, in your case it would be xCuenta .
In 2: If when creating the Activity we have a stored value, we get it and set it.
This question was answered in the English forum , I translated and adapted it: