2

Please Read My Question Carefully

I am learning Activity LifeCycle. I only have one EditText and a TextView in my XML layout With certain id. When I rotate the screen, nothing seems to change because Rotation doesn't affect EditText. But when I remove the id of EditText in XML and rotated the screen, The Rotation Starts affecting and the text in the editText removed due to rotation. I am confused about the relation of EditText with its Id.

I tried to explain the problem below in columns:

Explanation of below Column names

  • Having Id: Does EditText has id?
  • Rotation : Phone has Rotated or not
  • EditText : What happen to EditText After Rotation.

.

Having Id----------------Rotation---------------EditText

Yes                  Rotated            Does not change

No                   Rotated             Yes, Text Removed from editText

MainActivity.java is empty and main_activity.xml code is below:


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter here"
            android:inputType="textPersonName"
            android:textSize="26sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"

            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.371" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="85dp"
            android:layout_height="25dp"
            android:layout_marginTop="88dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

enter image description here

Sorry for using bad English and High-resolution pictures as I have no other ways. Best Regards

Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32

6 Answers6

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

Sagar
  • 23,903
  • 4
  • 62
  • 62
0

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.

denniz crypto
  • 451
  • 4
  • 10
0

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.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • EditText has not relation with rotation. Like if you rotate your device, the editText text would never change or lost But when you remove the id of EditText in XML and then rotate the device the text in editText got lost as shown in above last two pictures Now the question arises that what is the relation of EditText with an id that without id its text getting lost on rotation? – Shoaib Kakal Apr 19 '20 at 13:08
  • I have no idea about why this happens. – Hasan Bou Taam Apr 19 '20 at 13:11
  • you can use my approach and see if it still happens. – Hasan Bou Taam Apr 19 '20 at 13:12
  • Ok then please UpVote this question so others could see this quickly. Thanks – Shoaib Kakal Apr 19 '20 at 13:14
0

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.

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
0

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:

1- TextView's text disappearing when device rotated

2- Handle screen rotation without losing data - Android

MMG
  • 3,226
  • 5
  • 16
  • 43
  • Yes, I added that in `EditText` and it didn't work because my case is different from the one you provided the link. Please read my question again. – Shoaib Kakal Apr 19 '20 at 17:56
  • Okay,if data is assigned to it's id then why other type of data is not saved in id... For example, if i add a TextView and count button, when i will press on count button the value in textview will be increment while textview having its id the data of textview would disappear after rotation ... Hence android does not assign data to id. Am i right? – Shoaib Kakal Apr 20 '20 at 05:51
  • It's this way for edittext maybe, for textview you can use another things for example freezestext @ShoaibK. – MMG Apr 20 '20 at 05:57
0

Okay, the relation of one specific EditText item to its id is: "one to one" or "one to N". One EditText item in one layout.xml has one id, if you've configured it in the layout.xml. If you've defined more than one layout.xml's and the very same EditText item (having the same id) in more than one layout.xml's, then EditText has a "one to N" relation.

Janos Vinceller
  • 1,208
  • 11
  • 25