Reputation:
I have a simple log in Activity
with two texts and one button, one for email and the other for password.
Once the login button is pressed, a new Activity
called profile is launched. I hardcoded a bunch of info to match the input so the intent launches the second Activity
. This works fine.
However, when I press the back button on the phone it goes back to the login Activity
. But when I press the login button again, it shows the same results, even if I change the input:
If I input the email and the password that are already in the ArrayList
, it launches the second Activity
. Now if I go back to the login Activity
by pressing the back button on the phone, the email and password text box still retains the values. Now if I remove the password and press login, it shows the same results, the same profile I log in, whereas it must prevent me from logging in because the password is incorrect
Note: the hardcoded Infos are stored in an Arraylist
with a custom class, the custom class contains nothing but 5 strings for basic info. Moreover, this class implements Serializable
, hence granting the ability to pass the object of this custom class via Intent
.
Login Activity
:
package just.innovates.isra;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.hanks.htextview.base.HTextView;
import java.util.ArrayList;
public class LoginActivity extends AppCompatActivity {
public static final String EXTRA_LOGIN = "just.innovates.isra.EXTRA_LOGIN";
private ArrayList<Profile> arrayList = new ArrayList<>();
private Profile profile;
private EditText editTextEmail;
private EditText editTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.tv_login_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
editTextEmail = findViewById(R.id.et_login_email);
editTextPassword = findViewById(R.id.et_login_password);
arrayList.add(new Profile("Alaa", "Zarnaji", "[email protected]", "alaa", "01145874123", R.drawable.alaa));
arrayList.add(new Profile("Ola", "Ahmed", "[email protected]", "ola", "0123654585", R.drawable.ola));
arrayList.add(new Profile("Muhammad", "Magdy", "[email protected]", "magdy", "01014525874", R.drawable.magdy));
arrayList.add(new Profile("Isra", "Ahmed", "[email protected]", "isra", "01145214587", R.drawable.isra));
findViewById(R.id.btn_login_sign_in).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getProfile();
if (profile != null) {
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
intent.putExtra(EXTRA_LOGIN, profile);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Wrong Email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
private void getProfile() {
String email = editTextEmail.getText().toString();
String password = editTextPassword.getText().toString();
if (!email.trim().isEmpty() || !password.trim().isEmpty()) {
if (email.equals("[email protected]") && password.equals("alaa")) {
profile = arrayList.get(0);
} else if (email.equals("[email protected]") && password.equals("ola")) {
profile = arrayList.get(1);
} else if (email.equals("[email protected]") && password.equals("magdy")) {
profile = arrayList.get(2);
} else if (email.equals("[email protected]") && password.equals("isra")) {
profile = arrayList.get(3);
}
}
}
}
Profile Activity
:
package just.innovates.isra;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class ProfileActivity extends AppCompatActivity {
private TextView textViewNameFirst;
private TextView textViewNameLast;
private TextView textViewEmail;
private TextView textViewPassword;
private TextView textViewPhone;
private ImageView imageView;
private Profile profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Intent intent = getIntent();
if (intent.hasExtra(RegisterActivity.EXTRA_PROFILE)) {
profile = (Profile) intent.getSerializableExtra(RegisterActivity.EXTRA_PROFILE);
}else if (intent.hasExtra(LoginActivity.EXTRA_LOGIN)){
profile = (Profile) intent.getSerializableExtra(LoginActivity.EXTRA_LOGIN);
}
setUI();
assert profile != null;
if (profile.isImage()) {
imageView.setImageResource(profile.getImageURL());
} else {
imageView.setVisibility(View.GONE);
}
textViewNameFirst.setText(profile.getFirstName());
textViewNameLast.setText(profile.getSecondName());
textViewEmail.setText(profile.getEmail());
textViewPassword.setText(profile.getPassword());
textViewPhone.setText(profile.getPhone());
}
private void setUI() {
textViewNameFirst = findViewById(R.id.nameFirst);
textViewNameLast = findViewById(R.id.nameLast);
textViewEmail = findViewById(R.id.email);
textViewPassword = findViewById(R.id.password);
textViewPhone = findViewById(R.id.phone);
imageView = findViewById(R.id.image);
}
}
Upvotes: 1
Views: 79
Reputation: 1616
When you input the email and password contained in the ArrayList, and launch SecondActivity by pressing login, the profile
object stores those values. And then you press back, remove password and press login again all your check does is decide the value of profile
object...not actually interfere with login procedure. So when you go back and change values, your profile still holds the old value from the array you used earlier. You should add another else statement for when none of the conditions match and you make your profile null, so that the check in your listener shows the toast.
So add this final else statement in your getProfile()
method after all the else if statements
else {
profile = null;
}
Or instead of writing the else condition twice for when both are empty or fields don't match you can simply set it to null every time getProfile()
is called.
Method should look like this
private void getProfile() {
String email = editTextEmail.getText().toString();
String password = editTextPassword.getText().toString();
profile = null;
if (!email.trim().isEmpty() || !password.trim().isEmpty()) {
if (email.equals("[email protected]") && password.equals("alaa")) {
profile = arrayList.get(0);
} else if (email.equals("[email protected]") && password.equals("ola")) {
profile = arrayList.get(1);
} else if (email.equals("[email protected]") && password.equals("magdy")) {
profile = arrayList.get(2);
} else if (email.equals("[email protected]") && password.equals("isra")) {
profile = arrayList.get(3);
}
}
}
Upvotes: 1