R4chi7
R4chi7

Reputation: 863

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 there some way to retain the data so that it is not lost while changing the orientation?

I have created 2 layouts, one each for portrait and landscape. I also have the same IDs for all the Views in both the layouts.

Thanks in advance.

Upvotes: 1

Views: 3146

Answers (3)

Plato
Plato

Reputation: 2348

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.

Upvotes: 2

Avadhani Y
Avadhani Y

Reputation: 7626

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()

Upvotes: 2

Srinivasan
Srinivasan

Reputation: 4661

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

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

Thanks....

Upvotes: 0

Related Questions