We've just started to develop an application. We chose to connect to the Firebase database using an email user and password. Normally login and create new users, however, my Preferences class is not saved, and every time I close the application, it asks for the user and password again. How do I get it saved in the application's "memory"?
My Login class (called MainActivity):
package e.servidor.myapplication.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.SharedLibraryInfo;
import android.preference.Preference;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.prefs.Preferences;
import e.servidor.myapplication.DAO.ConfiguracaoFirebase;
import e.servidor.myapplication.Entidades.Usuarios;
import e.servidor.myapplication.Helper.Preferencias;
import e.servidor.myapplication.R;
public class MainActivity extends AppCompatActivity {
private Button buttonlogin;
private EditText editTextsenha;
private EditText editTextemail;
private TextView textViewcliqueaqui;
private FirebaseAuth autenticacao;
private Usuarios usuarios;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextemail = (EditText) findViewById(R.id.editTextemail);
editTextsenha = (EditText) findViewById(R.id.editTextsenha);
textViewcliqueaqui = (TextView) findViewById(R.id.textViewcliqueaqui);
buttonlogin = (Button) findViewById(R.id.buttonlogin);
buttonlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editTextemail.getText().toString().equals("") && !editTextsenha.getText().toString().equals("")) {
usuarios = new Usuarios();
usuarios.setEmail(editTextemail.getText().toString());
usuarios.setSenha(editTextsenha.getText().toString());
validarLogin();
} else {
Toast.makeText(MainActivity.this, "Por favor, preencha os campos de E-mail e Senha.", Toast.LENGTH_LONG).show();
}
}
});
textViewcliqueaqui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abreCadastroUsuario();
}
});
}
private void validarLogin(){
autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
autenticacao.signInWithEmailAndPassword(usuarios.getEmail(), usuarios.getSenha()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Login efetuado com sucesso.", Toast.LENGTH_LONG).show();
abrirTelaPrincipal();
} else {
Toast.makeText(MainActivity.this, "E-mail ou Senha incorretos.", Toast.LENGTH_LONG).show();
}finish();
}
});
}
public void abrirTelaPrincipal(){
Intent intentAbrirTelaPrincipal = new Intent(MainActivity.this, telaprincipal.class);
startActivity(intentAbrirTelaPrincipal);
}
public void abreCadastroUsuario(){
Intent intent = new Intent(MainActivity.this, cadastro.class);
startActivity(intent);
}
}
My Class Preferences:
package e.servidor.myapplication.Helper;
import android.content.Context;
import android.content.SharedPreferences;
public class Preferencias {
private Context context;
private SharedPreferences preferences;
private String NOME_ARQUIVO = "myapplication.preferences";
private int MODE=0;
private SharedPreferences.Editor editor;
private final String CHAVE_IDENTIFICADOR = "IdentificadorUsuarioLogado";
private final String CHAVE_EMAIL = "EmailUsuarioLogado";
public Preferencias(Context context) {
this.context=context;
preferences = context.getSharedPreferences(NOME_ARQUIVO,Context.MODE_PRIVATE);
editor.apply();
}
public void SalvarUsuarioPreferencias (String IdentificadorUsuario, String IdentificadorEmail) {
editor.putString(CHAVE_IDENTIFICADOR, IdentificadorUsuario);
editor.putString(CHAVE_EMAIL, IdentificadorEmail);
editor.commit();
}
public String getIdentificador(){
return preferences.getString(CHAVE_IDENTIFICADOR,null);
}
public String getEmail() {
return preferences.getString(CHAVE_EMAIL, null);
}
}
My Class Users:
package e.servidor.myapplication.Entidades;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Exclude;
import java.util.HashMap;
import java.util.Map;
import e.servidor.myapplication.DAO.ConfiguracaoFirebase;
public class Usuarios {
private String id;
private String email;
private String senha;
public Usuarios() {
}
public void salvar(){
DatabaseReference referenciaFirebase = ConfiguracaoFirebase.getFirebase();
referenciaFirebase.child("usuario").child(String.valueOf(getId())).setValue(this);
}
public Map<String, Object> toMap () {
HashMap<String, Object> HasMapUsuario = new HashMap<>();
HasMapUsuario.put("id", getId());
HasMapUsuario.put("email", getEmail());
HasMapUsuario.put("senha", getSenha());
return HasMapUsuario;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}