I was checking how it looks to debug an application on an actual device. Following code just changes the text of a button according to the number of times it is pressed repeatedly.
It was increasing perfectly, but as I tilted the device and it switched to landscape, the button changed its text to to initial state. I pressed again and tilted again, the value again got reset.
What is going on in background? and how to stop it?
b.setOnClickListener( new OnClickListener() {
int i = 1;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "Pressed ", Toast.LENGTH_SHORT).show();
b.setText("press "+ i);
i++;
}
});
When you tilt the device your activity is destroyed and oncreate is called again .
You have to retain the value you want to get back when its is restarted.
There are several ways to do it .
In shortcut you can just add a flag in Activity in manifest file which is android:configchanges = “orientation”.
Aur save your values in onPause or onBundleSaveinstance lifecycle callback methods.