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 */privatevoidsaveTextViewState(ViewGroup rootView, Bundle bundle){
    intchildren= rootView.getChildCount();
    for (inti=0; i < children; i++) {
        Viewview= rootView.getChildAt(i);
        if (view instanceof TextView){
            TextViewtxt= (TextView)view;
            if (txt.getText() != null){
                bundle.putString(keyForTextView(txt), txt.getText().toString());
            }

        }elseif (view instanceof ViewGroup){
            saveTextViewState((ViewGroup)view, bundle);
        }
    }
}


@OverrideprotectedvoidonSaveInstanceState(Bundle outState) {

    Viewroot= 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 */privatevoidloadTextViewState(ViewGroup rootView, Bundle bundle){
    intchildren= rootView.getChildCount();
    for (inti=0; i < children; i++) {
        Viewview= rootView.getChildAt(i);
        if (view instanceof TextView){
            TextViewtxt= (TextView)view;
            Stringsaved= bundle.getString(keyForTextView(txt));
            txt.setText(saved); 

        }elseif (view instanceof ViewGroup){
            loadTextViewState((ViewGroup)view, bundle);
        }
    }
}

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //... inflate your UI hereif (savedInstanceState != null){
         Viewroot= 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,

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

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

@SuppressWarnings("deprecation")
@OverridepublicObjectonRetainNonConfigurationInstance() {
    returnsuper.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,

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

Thanks....

Solution 4:

You need to overrideonSaveInstanceState(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.

@OverridepublicvoidonSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  EditTextmEdittext=(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.

@OverridepublicvoidonRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);

      StringmyString= savedInstanceState.getString("MyString");
     EditTextmEdittext=(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"