Relation of EditText with its Id in Android Studio?

大城市里の小女人 提交于 2020-04-30 09:19:55

问题


I am learning Activity LifeCycle. I only have one EditText and a TextView in my XML layout. When I rotate the screen, nothing seems to change. But when I remove the id of EditText in XML and rotated the screen, The text in the editText removed. I am confused about the relation of EditText with its Id.

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

Below, After Removing the id of EditText in XML, The text was removed when I rotated the screen.


    <?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>

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


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/61303820/relation-of-edittext-with-its-id-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!