Ayush Goyal
Ayush Goyal

Reputation: 2079

Integer value gets lost when screen orientation is changed?

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++;
    }
});

Upvotes: 0

Views: 801

Answers (6)

Blundell
Blundell

Reputation: 76536

Activity's are destroyed and recreated when the screen orientation is changed.

You can either lock it to one orientation.

or

Save the state of your application using onSaveInstanceState then restore it in onCreate or onRestoreInstanceState

Upvotes: 1

Lazy Ninja
Lazy Ninja

Reputation: 22537

You need to use onSaveInstanceState to save your data because data get lost when orientation changes. Google onSaveInstanceState and if you still have problems ask.

Upvotes: 2

Macchiato
Macchiato

Reputation: 815

Your activity is relaunched (onCreate is called) every time the activity changes orientation, to avoid this you can set in your androidmanifest android:configChanges="orientation" to the activity.

Upvotes: 0

Shachilies
Shachilies

Reputation: 1616

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".

<activity
            android:name=".Splash"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Aur save your values in onPause or onBundleSaveinstance lifecycle callback methods.

Upvotes: 3

Daud Arfin
Daud Arfin

Reputation: 2499

Yes, because your activity will be restarted. You need to save your data on orientation changes to saved instant or else you need to restrict orientation changes in your activity.

Upvotes: 2

Lucifer
Lucifer

Reputation: 29672

you need to assign android:screenOrientation="landscape" in AndroidManifest.xml, look at following code,

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Upvotes: 1

Related Questions