How to retain EditText data on orientation change?

后端 未结 12 1898
暗喜
暗喜 2020-12-05 13:34

I have a Login screen which consists of 2 EditTexts for Username and Password. My requirement is that on orientation change , input data(if any) in EditText should r

相关标签:
12条回答
  • 2020-12-05 14:08

    The following should work and is standard to the activities and fragments

    @Override
    public void onSaveInstanceState (Bundle outState) 
    {
         outState.putString("editTextData1", editText1.getText().toString());
         outState.putString("editTextData2", editText2.getText().toString());
    
         super.onSaveInstanceState(outState);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
          super.onCreate();
    
          ... find references to editText1, editText2
    
          if (savedInstanceState != null)
          {
               editText1.setText(savedInstanceState.getString("editTextData1");
               editText2.setText(savedInstanceState.getString("editTextData2");
          }
    }
    
    0 讨论(0)
  • 2020-12-05 14:11

    Give the element an id and Android will manage it for you.

    android:id="@id/anything"
    
    0 讨论(0)
  • 2020-12-05 14:19

    By default, Edittext save their own instance when changing orientation.

    Be sure that the 2 Edittexts have unique IDs and have the same IDs in both Layouts.

    That way, their state should be saved and you can let Android handle the orientation change.

    If you are using a fragment, be sure it has a unique ID also and you dont recreate it when recreating the Activity.

    0 讨论(0)
  • 2020-12-05 14:19

    Below code is work for me. Need to care two things.

    1. Each Input Field (Edit Text or TextInputEditText) assign unique id.
    2. Manifest activity declaration should have on configuration change attribute with below values.

      android:configChanges="orientation|keyboardHidden|screenSize"

    Sample activity declaration in manifest.

    <activity
          android:name=".screens.register.RegisterActivity"
          android:configChanges="orientation|keyboardHidden|screenSize"
          android:exported="true"
          android:label="Registration"
          android:theme="@style/AppTheme.NoActionBar" />
    

    Sample declaration of

     <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/inputLayout"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:boxCornerRadiusBottomEnd="@dimen/boxCornerRadiusDP"
            app:boxCornerRadiusBottomStart="@dimen/boxCornerRadiusDP"
            app:boxCornerRadiusTopEnd="@dimen/boxCornerRadiusDP"
            app:boxCornerRadiusTopStart="@dimen/boxCornerRadiusDP">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/inputEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:fontFamily="@font/proxima_nova_semi_bold"
                android:inputType="textCapWords"
                android:lines="1"
                android:textColor="@color/colorInputText"
                android:textColorHint="@color/colorInputText" />
        </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
  • 2020-12-05 14:20

    A better approach is to let android handle the orientation change. Android will automatically fetch the layout from the correct folder and display it on the screen. All you need to do is to save the input values of the edit texts in the onSaveInsanceState() method and use these saved values to initialize the edit texts in the onCreate() method.
    Here is how you can achieve this:

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        ...
        ...
        String userName, password;
        if(savedInstanceState!=null)
        {
            userName = savedInstanceState.getString("user_name");
            password= savedInstanceState.getString("password");
        }
    
        if(userName != null)
            userNameEdtTxt.setText(userName);
        if(password != null)
            passEdtTxt.setText(password);
    }
    

    >

    @Override
        protected void onSaveInstanceState (Bundle outState)
        {
            outState.putString("user_name", userNameEdtTxt.getText().toString());
            outState.putString("password",  passEdtTxt.getText().toString());
        }
    
    0 讨论(0)
  • 2020-12-05 14:21

    Im restoring instance to restore values and it works fine for me :)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtask2);
        if(savedInstanceState!=null)
         onRestoreInstanceState(savedInstanceState);
    
    }
    
    0 讨论(0)
提交回复
热议问题