I need the variable xCount not reset to 0, eg when I rotate the screen

0
public class MainActivity extends AppCompatActivity {

        // defino variables de botones y texto
        ImageButton imgButtonUp;
        ImageButton imgButtonDw;
        ImageButton imgButtonRes;
        ImageButton imgButtonInfo;
        ToggleButton toogleB;
        TextView textCount;
        MediaPlayer mediaPlayer;
        int xCuenta=0;

         @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    // defs
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

    // Defino variables
            textCount = (TextView) findViewById(R.id.txtTurno);
            imgButtonUp = (ImageButton) findViewById(R.id.ibuUp);
            imgButtonDw = (ImageButton) findViewById(R.id.ibuDw);
            imgButtonRes = (ImageButton) findViewById(R.id.ibuRes);
            imgButtonInfo =(ImageButton) findViewById(R.id.ibuInfo);
            toogleB = (ToggleButton) findViewById(R.id.tgSound );

    // Defino Sonido ding dong
            mediaPlayer = MediaPlayer.create(this, R.raw.ding2);

            // onclick del boton Up
            imgButtonUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    // incremento cuenta
                    xCuenta++;
                    if (xCuenta > 99) {
                        xCuenta = 0;
                    }
    // convierto y presento
                    textCount.setText(String.format(Locale.US,"%02d", xCuenta));

    // Reprocir sound
                    mediaPlayer.start();

                }
            });
    
asked by Alejandro Bergesio 21.07.2016 в 01:14
source

1 answer

0

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:

    
answered by 21.07.2016 в 07:54