1

I have created a signup form linked with Firebase. Onclick Signup button generated an unique id which I have later stored in

                            String str = userid;

But now I want to pass this String str value back into my main activity so that I can get that unique id and can later on use in my other code.

I have tried many methods like bundle and passing string through intent but I'm getting nothing. Please help me resolving this issue.

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

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();


    btnSignIn = (Button) findViewById(R.id.sign_in_button);
    btnSignUp = (Button) findViewById(R.id.sign_up_button);
    inputName = (EditText) findViewById(R.id.name);
    inputEmail = (EditText) findViewById(R.id.email);
    inputNumber = (EditText) findViewById(R.id.number);
    inputPassword = (EditText) findViewById(R.id.password);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnResetPassword = (Button) findViewById(R.id.btn_reset_password);

    btnResetPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            rootNode = FirebaseDatabase.getInstance();
            reference = rootNode.getReference("users");
            final String userid = reference.push().getKey();
            final String name = inputName.getText().toString().trim();
            final String email = inputEmail.getText().toString().trim();
            final String Number = inputNumber.getText().toString().trim();
            final String password = inputPassword.getText().toString().trim();

            if (TextUtils.isEmpty(name)) {
                Toast.makeText(getApplicationContext(), "Enter your name!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(Number)) {
                Toast.makeText(getApplicationContext(), "Enter mobile number!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 6) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                return;
            }

            final UserHelperClass helperClass = new UserHelperClass(name, email,Number,password,userid);
            progressBar.setVisibility(View.VISIBLE);
            //create user
            auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            } else {

                                String str = userid;


                                reference.child(userid).setValue(helperClass);
                                startActivity(new Intent(SignupActivity.this, Home.class));
                                finish();
                            }
                        }

                    });

        }
    });
}          

2 Answers2

0

You can start your second activity saying startActivityForResult (See here), this way you can pass strings, numbers... back to the MainActivity from where you started your second activity once your second activity finishes.

Like First Activity:

Intent i = new Intent(getApplicationContext(), yourSecondActivity.class); 
i.putExtra("name", "value");
startActivityForResult(i, requestCode);

Second activiy:

Intent output = new Intent();
output.putExtra("name", "value");
setResult(RESULT_OK, output);
finish();

To recieve output in first Activity

protected void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode, resultCode, data);
      String value = data.getStringExtra("name");
Legend Bard
  • 161
  • 3
0

You can use SharedPreferences API to save this string to an XML file in the second activity and read it from the same XML file in the MainActivity.

To save the string:

SharedPreferences sharedPreferences = getSharedPreferences("shared_pref", 0);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("key", userid);
editor.apply();

To retrieve the string:

SharedPreferences sharedPreferences = getSharedPreferences("shared_pref", 0);
String userid = sharePreferences.getString("key", "NOT FOUND");

You can read more about SharedPreferences here