0

I have 25 edittexts and other views on my activity. I have created different main.xml files in layout and layout-land, so the respective UI will be displayed according to the switched mode(either portrait or landscape).

But After filling values in the edittexts, if I change from the portrait to landscape, the previous values are lost. So, my doubt is how to get the values from the edittexts and restore the values at their respective edittexts even after changing to other mode.

Of course I know when we switch modes, the activity is recreated which results the data loss. I have revised even Handling Runtime changes in the developer guide, but as a newbie I am unable to understand clearly like onRetainNonConfigurationInstance() method to save data as it returns object, but in my app I need the numbers that are entered in the edittexts to be on their respective edittexts even after switching.

Have referred lot of questions like link etc., even on stackoverflow, but I am unable to find solution for my problem. Please suggest the solution for this.

Even I have another problem. I have a dialog which pops up when this activity is first started and I don't want this too to popup while switching modes. Please suggest the solution for these two. If anyone needs code snippet for more clarification, please let me know. I will edit my question with the code.

Community
  • 1
  • 1
Spike
  • 147
  • 3
  • 17

2 Answers2

1

If you have a less data you can save and get it using onSavedInstanceState and onRestoreInstanceState.. for details go through this link Saving Data

But incase you have large data then I must say you should not allow for the orientation changes you can restrict it by adding below line in manifest file :

android:configChanges="orientation|keyboardHidden"

Above line fixes orientation but in this case your layout-land will not work but you can handle or repositioned your view dynamically inside onConfigurationChanged() method

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

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        align_landscape(); // align your view dynamically here for landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        align_portrait(); // align your view dynamically here for portrait
    }
}
Community
  • 1
  • 1
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • Hi Arfin. Thanks a lot for your response. Your explanation is very clear. But here I have large data. Adding my view dynamically is very complex. Becuase I have 25 edittexts and 10 buttons which make a custom keypad. Is it possible to get the solution in another way? Do you have any idea what is onRetainNonConfigurationInstance() meant for. It returns object but I need values. – Spike Sep 10 '12 at 13:37
  • I have used setContentView(R.layout.main); in the onConfigurationChanged() method. So the view is changed to the respective mode. The problem is I am able to retrieve the values but I am unable to set the values in the edittexts. Please reply. – Spike Sep 11 '12 at 14:16
0

You can store the edit text values when the orientation changes and then switch to the new layout and assign the saved values.

By specifying the following condition in the manifest, you can detect the orientation change.

android:configChanges="orientation"

in you onConfigurationChanged() method, store the values of the various fields and then you can change the orientation using the setRequestedOrientation(int requestedOrientation) method which will update to your custom layout, then restore the values of your fields.

Gan
  • 1,349
  • 2
  • 10
  • 27
  • Thank you Gan. Will let you know if this works. What about Dialog reappearing problem? – Spike Sep 10 '12 at 10:52
  • could you elaborate on the pop-up dialog. Are you make a toast or is it generated by the system? – Gan Sep 10 '12 at 10:59
  • I just have a dialog before the main activity starts which asks the user some choices to select. But when I change the mode, even the dialog is also appearing every time. – Spike Sep 10 '12 at 11:07
  • When the orientation changes, the onCreate method gets called again, so the dialog is displayed. If you follow the idea that I have suggested you can avoid that. – Gan Sep 10 '12 at 11:23
  • Gan,I have used setContentView(R.layout.main); in the onConfigurationChanged() method. So the view is changed to the respective mode. The problem is I am able to retrieve the values but I am unable to set the values in the edittexts. Please reply. – Spike Sep 11 '12 at 14:17
  • use the setText() method of the EditText to assign the stored values. – Gan Sep 11 '12 at 15:15
  • Yes, I used, but didn't work. I found the reason for that. But unable to solve. Can you pls come to chat? – Spike Sep 12 '12 at 02:58