Поэтому меня беспокоит то, что я использую Shared Preference для сохранения имени в Android и также могу получить то же самое при повторном входе в систему. Но когда какой-то другой человек входит в систему с того же устройства, сохраненное имя остается прежним. пользователь. Как я могу изменить это и получить значение New user name из firebase? P.S. Я новичок в Android
Ниже приведен мой код для входа в систему: -
public class ManualLogin extends AppCompatActivity implements View.OnClickListener{
private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignup;
private EditText editTextName;
DatabaseReference databaseUsers;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
private static final String TAG = "FACELOG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manual_login);
databaseUsers = FirebaseDatabase.getInstance().getReference("Users");
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()!=null){
//profile activity here
finish();
startActivity(new android.content.Intent(getApplicationContext(), AccountActivity.class));
}
progressDialog = new ProgressDialog(this);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
editTextEmail = (EditText)findViewById(R.id.editTextEmail);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
editTextName = (EditText)findViewById(R.id.editTextName);
textViewSignup = (TextView)findViewById(R.id.textViewSignup);
buttonRegister.setOnClickListener(this);
textViewSignup.setOnClickListener(this);
}
public void registerUser() {
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
final String name = editTextName.getText().toString().trim();
if(!TextUtils.isEmpty(name)) {
String id = databaseUsers.push().getKey();
Users users = new Users(id, name);
databaseUsers.child(id).setValue(users);
//Toast.makeText(this, "User Created", Toast.LENGTH_SHORT).show();
}else{
//email is empty
Toast.makeText(this, "Enter Name", Toast.LENGTH_SHORT).show();
//stop function execution
return;
}
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this, "Enter Email id", Toast.LENGTH_SHORT).show();
//stop function execution
return;
}
if(TextUtils.isEmpty(password)){
//password is empty
Toast.makeText(this, "Enter Password", Toast.LENGTH_SHORT).show();
//stop function execution
return;
}
//if validations are fine
//show progressBar
progressDialog.setMessage("Registering User...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//user is successfully registered & logged in
//profile activity here
finish();
startActivity(new android.content.Intent(getApplicationContext(), AccountActivity.class));
userProfile();
Toast.makeText(ManualLogin.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(ManualLogin.this, "Failed to Register", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
SharedPreferences sharedPref = getSharedPreferences("userName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", editTextName.getText().toString());
editor.commit();
//Toast.makeText(ManualLogin.this, "Name Saved", Toast.LENGTH_SHORT).show();
}
});
}
//set User Display name
private void userProfile(){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null){
UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
.setDisplayName(editTextName.getText().toString().trim()).build();
user.updateProfile(profileUpdate).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.d("Testing", "User Profile Updated");
}
}
});
}
}
@Override
public void onClick(View v) {
if(v == buttonRegister){
registerUser();
}
if(v == textViewSignup){
//open login activity
finish();
startActivity(new android.content.Intent(this, LoginActivity.class));
}
}
}
Ниже приведен код для AccountActivity: -
public class AccountActivity extends AppCompatActivity {
private Button logout;
private TextView textViewUserName;
private FirebaseAuth mAuth;
private FirebaseAuth firebaseAuth;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
logout = (Button)findViewById(R.id.logout);
textViewUserName = (TextView)findViewById(R.id.textViewUserName);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this, LoginActivity.class));
}
FirebaseUser sname = firebaseAuth.getCurrentUser();
textViewUserName.setText("Welcome "+ sname.getDisplayName());
SharedPreferences sharedPref = getSharedPreferences("userName", Context.MODE_PRIVATE);
String name = sharedPref.getString("name", "");
textViewUserName.setText("Welcome "+ name);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
LoginManager.getInstance().logOut();
updateUI();
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
updateUI();
}
}
private void updateUI() {
Toast.makeText(this, "You have Logged out", Toast.LENGTH_SHORT).show();
Intent accountIntent = new Intent(this, MainActivity.class);
startActivity(accountIntent);
finish();
}
}