I have a login screen in the application, and I want to show an error in the input and for this I use a pop-up message and an animated button. Here is how everything is implemented:
public void onResponse(@NonNull Call<GetToken> call, @NonNull Response<GetToken> response) { if (response.isSuccessful()) { custombtn.showSuccess(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(LoginActivity.this, MainScreen.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); finish(); overridePendingTransition(0, 0); } }, 1500); PorterDuff.Mode.MULTIPLY); } else { custombtn.showError(); custombtn.setBackgroundColor(Color.parseColor("#FF0000")); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(LoginActivity.this, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("login", login); intent.putExtra("password", password); startActivity(intent); } }, 1500); ResponseBody errorBody = response.errorBody(); try { if (Objects.requireNonNull(errorBody).string().contains("no_data_passed")) { Toast.makeText(LoginActivity.this, "Please enter some data!", Toast.LENGTH_LONG).show(); } else if (Objects.requireNonNull(errorBody).string().contains("authentication_failed")) { Toast.makeText(LoginActivity.this, "Your authentication failed!", Toast.LENGTH_LONG).show(); //submitBtn.getBackground().setColorFilter(Color.parseColor("#1cd000"), PorterDuff.Mode.MULTIPLY); } } catch (IOException e) { e.printStackTrace(); } } but there is one problem, after the reboot of the activation, I lose the data from the input fields and I need to enter them again. I read that you can use this:
@Override protected void onSaveInstanceState(Bundle savedData) { etLogin = findViewById(R.id.login); etPassword = findViewById(R.id.password); login = etLogin.getText().toString().trim(); password = etPassword.getText().toString().trim(); savedData.putString("login", login); savedData.putString("password", password); super.onSaveInstanceState(savedData); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.w("MY_TAG", "login" + savedInstanceState.getString("login")); Log.w("MY_TAG", "password" + savedInstanceState.getString("password")); } that is, we save the activation state, and then roll it back when we have a reload of the view. But for some reason this did not help as I did not try. I tried to do it through the intent, but there I constantly checked incoming data and got an error. I can not understand how to make the data saved and display this data again in the field.
SharedPreferenceon theEditTextlistener is Jarvis_Jedt.addTextChangedListener(new TextWatcher() {...}). It depends on what level of security you need ... if in the depth of your phone your passwords are stored, then, in my opinion, it is not critical) - Jarvis_J