Miss Lc
Miss Lc

Reputation: 69

Android screen orientation changed, textviews that are programmatically added are not saved

Basically I have two activities. The main activity has a button that goes to the second activity which specifies the name of the textview (three textviews to be specific). After that, going back the the main activity, the specified name of the textview will be displayed on the first activity but the textviews are added programmatically and is added on to different tablerows based on its name. My problem is that when the screen orientation is changed from portrait to landscape, the textviews that are programatically added are not saved. How would I save those values? I've tried android:configChanges but it doesn't save anything at all.

Upvotes: 1

Views: 936

Answers (4)

Maksim Dmitriev
Maksim Dmitriev

Reputation: 6209

As far as I understand, in the second activity you need to specify the text which will assigned to a few text views in the first aka main activity.

I would recommend the two ways:

  1. Use savedInstanceState, onSaveInstanceState() to save and extract the strings of your text views you've set programmatically.
  2. Apply android:configChanges="orientation|screenSize" to the main activity to prevent it from being recreated.

There are the first way and the second way on Gist. And you can also see the diff here (2 changed files with 4 additions and 17 deletions).

Upvotes: 1

Nhut Duong
Nhut Duong

Reputation: 612

override onSaveInstanceState method and put all values you want to save. Get them back in onRestoreIntanceState method.

UPDATED

Save needed values

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString(KEY, VALUE);
...
}

Restore values

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState != null){
        String str = savedInstanceState.getString(KEY);
        //Set str to your text view
    }
}

Upvotes: 0

Techfist
Techfist

Reputation: 4344

android by default will recreate your UI upon configuration change(in your case, screen rotation change), now since your textviews/widgets are added in run time, they wont be recreated by android, as they are not available in XML layout which android will re inflate and add.

for your solution you do this, save the values your dynamically added views has by implementing onSaveInstanceState, use those view ID as key while storing the values(probably use row_id as views id).

now when configuration changes, based on saved values recreate your view and populate them with saved values, i guess this info might help you resolve this.

Upvotes: 0

baronS
baronS

Reputation: 1084

Put the code that adds the textViews in a method and call it in onCreate and also in onConfigurationChanged

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    addTextViews();
}

Upvotes: 0

Related Questions