1

I have created a login screen. If I type something on my edit text and rotate my device, the data gets lost. How can I make my edit texts, survive these changes?

I have created a loginViewModel in which I am just fetching the data from the api.

Here is my loginActivity

public class LogInActivity extends AppCompatActivity {
    private TextInputLayout mLoginUserName, mLoginPassword;
    private LoginViewModel loginViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        loginViewModel = new LoginViewModel(getApplication(), 
this);


        mLoginPassword = findViewById(R.id.logInPassword);
        mLoginUserName = findViewById(R.id.logInUserName);
        Button mFinalLoginButton =  
findViewById(R.id.finalLogInButton);
        TextView mForgotPassword = 
findViewById(R.id.text_view_forgot_password);
        mForgotPassword.setOnClickListener(new 
View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LogInActivity.this, 
ForgotPasswordSend.class));
            }
        });

        mFinalLoginButton.setOnClickListener(new 
View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = 
mLoginUserName.getEditText().getText().toString().trim();
                final String userPass = 
mLoginPassword.getEditText().getText().toString().trim();

                if (userName.isEmpty())
                    mLoginUserName.setError("Please enter User 
Name");
                else mLoginUserName.setError(null);

                if (userPass.isEmpty()) {
                    mLoginPassword.setError("Please enter 
password");
                } else {
                    mLoginPassword.setError(null);
                    loginViewModel.logInRequest(userName, 
userPass);
                }
            }


        });

    }}

This is my loginViewModel. Right now it is not working as a viewmodel. I just have named it but i want to make it work as a viewmodel class

class LoginViewModel extends AndroidViewModel {
private final Context context;
private final SharedPrefs sharedPrefs = new SharedPrefs(getApplication());


public LoginViewModel(@NonNull Application application, Context context) {
    super(application);
    this.context = context;
}

public void logInRequest(final String userName, String userPassword) {
    final JSONObject jsonObject = new JSONObject();
    try {

        jsonObject.put("identifier", userName);
        jsonObject.put("password", userPassword);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    APIService apiService = RetrofitClient.getAPIService();
    Call<String> logInResponse = apiService.logIn(jsonObject.toString());
    logInResponse.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.message().equals("OK")) {
                try {
                    JSONObject jsonObjects = new JSONObject(response.body());

                    JSONObject token = jsonObjects.getJSONObject("token");

                    String key = token.getString("token");
                    String sUserName = jsonObjects.getString("username");
                    String email = jsonObjects.getString("email");
                    String firstName = jsonObjects.getString("firstName");
                    String lastName = jsonObjects.getString("lastName");

                    sharedPrefs.saveUserName(sUserName);
                    sharedPrefs.saveEmail(email);
                    sharedPrefs.saveFullName(firstName, lastName);
                    sharedPrefs.saveToken(key);
                    context.startActivity(new Intent(context, HomeActivity.class));


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                Toast.makeText(getApplication(), "Wrong User Name or Password", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Toast.makeText(getApplication(), "Something went wrong 
please try again", Toast.LENGTH_SHORT).show();

            }
        });

    }}
MukulCode
  • 97
  • 11
  • Possible duplicate of [How to retain EditText data on orientation change?](https://stackoverflow.com/questions/12420396/how-to-retain-edittext-data-on-orientation-change) – Taseer Jul 28 '19 at 15:28
  • I have checked this, but no one is talking about the livedata and viewmodel. – MukulCode Jul 28 '19 at 18:19
  • You can create an object to store edit text data in it and observe it. Why exactly would you need a `ViewModel` for that? Simply setting an ID to it makes the edit text survive config change – Taseer Jul 28 '19 at 18:35

0 Answers0