Relation Of EditText With Its Id In Android Studio?
Solution 1:
One of the most important aspects in Android programming is to handle the configuration changes and most common of them is orientation changes. Although it is not recommended to handle configuration changes yourself, due to the hidden complexity of handling the configuration changes, there are cases where we might need to handle manually.
If you need to save the state, the common approach is to implement onSaveInstanceState
to store & restore the values which are passed as Bundle.
Coming back to your question:
Relation of EditText with its Id in Android Studio?
When it comes to saving state of View
Android OS automatically calls the View.onSaveInstanceState
method on each of the views in your view hierarchy as long as you call through to the super method in onSaveInstanceState
. In order to restore the data, it uses android:id
attribute key for that particular View’s state.So for your case, when you don't provide the id
for EditText
, the View's state won't be stored due to missing key.
Hope this clarifies the relationship of id
to EditText
Solution 2:
When you rotate screen on android all activities and fragments in your application are destroyed and then recreated. Android does this so that your application can reload resources based on the new configuration.
When the system destroys the activity (e.g. when the screen rotates) then although the actual Activity instance is gone, the system remembers that it existed. The system creates a new instance of that activity using a set of saved data that describes the state of the activity when it was destroyed.
when your assign an id to a view it seems that android keeps track of that view. Hence it remembers it's state.
Solution 3:
Okay when you rotate the device this is considered as a configuration change, so the system recreates the activity and data is lost.
One way to avoid this is go to the manifest and for your specific activity add this
android:configChanges="orientation|screenSize"
here:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize"
....
...
This makes the system ignore the rotation of device.
Solution 4:
In manifest of your app, add this line to activity tag:
android:configChanges="keyboardHidden|orientation|screenSize"
The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.
When you use id
for your component, data will be assigned to its id
, but when it hasn't id
, it will be recreated and data will be disappeared after rotation.
See these questions and read their good answers, I don't copy and paste them:
Solution 5:
EditText
is a focused view, so in PhoneWindow
, it's state will be saved automatically in saveHierarchyState()
method. Only EditText
with id is going to save text.
Post a Comment for "Relation Of EditText With Its Id In Android Studio?"