I'm a beginner with Android Studio so I don't know much about how stuff works in it. I wrote some code for an app which uses Firebase Authentication for email/password user logging in.
This is shown in logcat as I click the 'Login' button: W/System: Ignoring header X-Firebase-Locale because its value was null.
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email_id = emailLogin.getText().toString();
String password_id = passwordLogin.getText().toString();
boolean dataIsValidated = validateData(email_id, password_id);
if (!dataIsValidated) {
return;
} else {
loginAccount(email_id, password_id);
}
}
void loginAccount(String email_id,String password_id){
changeInProgress(true);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.signInWithEmailAndPassword(email_id,password_id).addOnCompleteListener(task -> {
if(task.isSuccessful()){
if(firebaseAuth.getCurrentUser().isEmailVerified()){
Toast.makeText(Actual_Login.this,"Logged in successfully!",Toast.LENGTH_SHORT).show();
startActivity(intentToHome);
}else{
Toast.makeText(Actual_Login.this,"This email isn't verified! Sign up with this email first",Toast.LENGTH_SHORT).show();
changeInProgress(false);
}
}else{
Toast.makeText(Actual_Login.this,"Login failed!",Toast.LENGTH_SHORT).show();
changeInProgress(false);
}
});
}
}
But the app stops after showing the toast message "Logged in successfully!". The app is logged in as if I open it again, it closes again after the splash screen. Here's the full Java file which the code block above attached belongs to:
package com.brilliantbrushes.safe;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Actual_Login extends AppCompatActivity {
TextView movetosignup;
EditText emailLogin,passwordLogin;
Button loginbtn;
ProgressBar progressLogin;
private Intent intentToHome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actual_login);
Intent intentToHome = new Intent(Actual_Login.this,Home_Page.class);
emailLogin=findViewById(R.id.emailLogin);
passwordLogin=findViewById(R.id.passwordLogin);
loginbtn=findViewById(R.id.loginbtn);
progressLogin=findViewById(R.id.progressLogin);
movetosignup = findViewById(R.id.movetosignup);
movetosignup.setOnClickListener(view -> {
Intent intent = new Intent(Actual_Login.this, Login_Page.class);
startActivity(intent);
});
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email_id = emailLogin.getText().toString();
String password_id = passwordLogin.getText().toString();
boolean dataIsValidated = validateData(email_id, password_id);
if (!dataIsValidated) {
return;
} else {
loginAccount(email_id, password_id);
}
}
void loginAccount(String email_id,String password_id){
changeInProgress(true);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.signInWithEmailAndPassword(email_id,password_id).addOnCompleteListener(task -> {
if(task.isSuccessful()){
if(firebaseAuth.getCurrentUser().isEmailVerified()){
Toast.makeText(Actual_Login.this,"Logged in successfully!",Toast.LENGTH_SHORT).show();
startActivity(intentToHome);
}else{
Toast.makeText(Actual_Login.this,"This email isn't verified! Sign up with this email first",Toast.LENGTH_SHORT).show();
changeInProgress(false);
}
}else{
Toast.makeText(Actual_Login.this,"Login failed!",Toast.LENGTH_SHORT).show();
changeInProgress(false);
}
});
}
});
}
void changeInProgress(boolean inProgress){
if(inProgress){
progressLogin.setVisibility(View.VISIBLE);
loginbtn.setVisibility(View.GONE);
}else{
progressLogin.setVisibility(View.GONE);
loginbtn.setVisibility(View.VISIBLE);
}
}
boolean validateData(String email_id,String password_id){
if (!Patterns.EMAIL_ADDRESS.matcher(email_id).matches()){
emailLogin.setError("The email used is invalid!");
return false;
}
if(password_id.length()<8){
passwordLogin.setError("Password must be at least 8 characters long!");
return false;
}
return true;
}
};
I tried removing the finish() at the end of intentToHome. I tries cleaning and rebuilding the app. Nothing reached anywhere.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Safe"
tools:targetApi="31">
<activity
android:name=".Add_Note"
android:exported="false" />
<activity
android:name=".Splash_Screen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Actual_Login"
android:exported="false" />
<activity
android:name=".Home_Page"
android:exported="false" />
<activity
android:name=".Login_Page"
android:exported="false"></activity>
</application>
</manifest>