Reputation: 147
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.
Upvotes: 0
Views: 1302
Reputation: 2499
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
}
}
Upvotes: 1
Reputation: 1399
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.
Upvotes: 0