使用firebase数据库正确存储用户信息时打开特定活动

时间:2018-03-12 22:42:08

标签: android firebase-realtime-database

使用firebase处理一些基本的数据库内容。我正确地保存了信息并登录和注册工作,但我现在想要实现的是,当用户在配置文件活动中成功注册信息时,它将打开MainMenu活动,并在下次登录应用程序时将知道他们已经将他们的用户信息保存在数据库中并打开MainMenu活动而不是配置文件活动。 多谢你们。 这是我的代码。

这是用户登录的主要活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignin;

private ProgressDialog progressDialog;

private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firebaseAuth = FirebaseAuth.getInstance();
    progressDialog = new ProgressDialog(this);

    if(firebaseAuth.getCurrentUser() != null){
        //Profile activity
        finish();
        startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
    }

    buttonRegister = (Button) findViewById(R.id.buttonRegister);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);
    textViewSignin = (TextView) findViewById((R.id.textViewSignin));

    buttonRegister.setOnClickListener(this);
    textViewSignin.setOnClickListener(this);
}

private void registerUser(){
    String email = editTextEmail.getText().toString().trim();
    String password = editTextPassword.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email is empty
        Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
        //stopping the function execution further
        return;
    }

    if(TextUtils.isEmpty(password)){
        //email is empty
        Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
        //stopping the function execution further
        return;
    }

    //if validations are Okay
    //Register User
    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
                       // Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_LONG).show();
                        finish();
                        startActivity((new Intent(getApplicationContext(), ProfileActivity.class)));
                    }
                    else{
                        Toast.makeText(MainActivity.this, "Could not register, Please try again!", Toast.LENGTH_LONG).show();
                    }
                    progressDialog.dismiss();
                }
            });
}

@Override
public void onClick(View view) {
    if(view == buttonRegister) {
        registerUser();
    }

    if (view == textViewSignin){
        //open login activity here
        startActivity(new Intent(this,LoginActivity.class));
    }
}
}

这是个人资料活动

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {


private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;

private TextView textViewUserEmail;
private Button buttonLogout, saveUserInformationBtn;
EditText userName, userAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    firebaseAuth = FirebaseAuth.getInstance();

    if (firebaseAuth.getCurrentUser() == null) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
    }

    databaseReference = FirebaseDatabase.getInstance().getReference();

    FirebaseUser user = firebaseAuth.getCurrentUser();

    textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
    saveUserInformationBtn = (Button) findViewById(R.id.userInfoSave);
    userName = (EditText) findViewById(R.id.userName);
    userAddress = (EditText) findViewById(R.id.userAddress);


    textViewUserEmail.setText("Welcome " + user.getEmail());

    buttonLogout = (Button) findViewById(R.id.buttonLogout);

    buttonLogout.setOnClickListener(this);
    saveUserInformationBtn.setOnClickListener(this);
}

private void saveUserInformation(){
    String name = userName.getText().toString().trim();
    String address = userAddress.getText().toString().trim();

    Userinformation userinformation = new Userinformation(name, address);

    FirebaseUser user = firebaseAuth.getCurrentUser();

    databaseReference.child(user.getUid()).setValue(userinformation);

    Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG). show();
}

@Override
public void onClick(View view) {
    if(view == buttonLogout){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(this,LoginActivity.class));
    }

    if (view == saveUserInformationBtn){
        saveUserInformation();
    }
}
}

0 个答案:

没有答案