Skip to content Skip to sidebar Skip to footer

Retaining Data On Screen Orientation Change

I have an activity that has a huge form with lots of EditTexts. If the user rotates the screen from portrait to landscape or vice-versa, all the data in the EditTexts is lost. Is t

Solution 1:

Save your state like this by implementing the following methods in your activity

/* simple method to create a key for a TextView using its id */
private String keyForTextView(TextView txt){
    return "textView"+txt.getId();
}


/* go through all your views in your layout and save their values */
private void saveTextViewState(ViewGroup rootView, Bundle bundle){
    int children = rootView.getChildCount();
    for (int i = 0; i < children; i++) {
        View view = rootView.getChildAt(i);
        if (view instanceof TextView){
            TextView txt = (TextView)view;
            if (txt.getText() != null){
                bundle.putString(keyForTextView(txt), txt.getText().toString());
            }

        }else if (view instanceof ViewGroup){
            saveTextViewState((ViewGroup)view, bundle);
        }
    }
}


@Override
protected void onSaveInstanceState(Bundle outState) {

    View root = findViewById(R.id.my_root_view); //find your root view of your layout
    saveTextViewState(root, outState); //save state 

    super.onSaveInstanceState(outState);
}

and then retrieve the values in the onCreate method of your activity.

 /* go through all your views in your layout and load their saved values */
 private void loadTextViewState(ViewGroup rootView, Bundle bundle){
    int children = rootView.getChildCount();
    for (int i = 0; i < children; i++) {
        View view = rootView.getChildAt(i);
        if (view instanceof TextView){
            TextView txt = (TextView)view;
            String saved = bundle.getString(keyForTextView(txt));
            txt.setText(saved); 

        }else if (view instanceof ViewGroup){
            loadTextViewState((ViewGroup)view, bundle);
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //... inflate your UI here

    if (savedInstanceState != null){
         View root = findViewById(R.id.my_root_view); //find your root view
         loadTextViewState(root, savedInstanceState); //load state 
    }
}

For this to work all the textboxes must have ids in both landscape and portrait layouts.


Solution 2:

Yes. You can get the data of EditTexts using below code:

In onCreate() method,

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.<your_landscape_layout>);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.<your_portrait_layout>);
        break;
    }
    setButtonClickListeners();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:

        setContentView(R.layout.<your_landscape_layout>);
        setButtonOnClickListeners();
        initializeViews();
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.<your_portrait_layout>);
        setButtonOnClickListeners();
        initializeViews();
        break;
    }
}

@SuppressWarnings("deprecation")
@Override
public Object onRetainNonConfigurationInstance() {
    return super.onRetainNonConfigurationInstance();
}
 }

In setButtonOnClickListeners(), you initiatiate all the EditTexts and in initializeViews() get the data of EditTexts and display using setText()


Solution 3:

Add android:configChanges="orientation" in your manifest file. Within tag,

    <activity
        android:name=".YourActivity"
        android:configChanges="orientation|keyboardHidden" >
    </activity>

Thanks....


Solution 4:

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

this method Save UI state changes to the savedInstanceState. This bundle will be passed to onCreate if the process is killed and restarted.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  EditText mEdittext =(EditText) findViewById(R.id.YourEdittextId);
  savedInstanceState.putString("MyString", mEdittext.getText().toString());
  ...
}

The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate and also onRestoreInstanceState where you'd extract the values like this:

this method Restore UI state from the savedInstanceState This bundle has also been passed to onCreate.

   @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);

      String myString = savedInstanceState.getString("MyString");
     EditText mEdittext =(EditText) findViewById(R.id.YourEdittextId);
      mEdittext.setText(myString);
    }

You have to use this technique to store instance values for your application (selections, text, etc.) for kno more Info refer this Link


Post a Comment for "Retaining Data On Screen Orientation Change"