title: How to save the UID of a user on registering the user using firebase Auth
body: <p>I'm trying to save the UID of a user on registering the user using Firebase Auth.</p>
  
  <p>Here I have already pushed the user UID as the key in the database under that I need to insert the same UID as a data</p>
  
  <pre><code>package com.example.loginregistration;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.fragment.app.DialogFragment;
  
  import android.app.DatePickerDialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.DatePicker;
  import android.widget.ImageView;
  import android.widget.RadioButton;
  import android.widget.Spinner;
  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 com.google.firebase.auth.FirebaseUser;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  import com.rengwuxian.materialedittext.MaterialEditText;
  
  import java.text.DateFormat;
  import java.util.Calendar;
  
  public class AddEmployee extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
  
      MaterialEditText Username, email, password, mobile, dob, job, department;
      RadioButton radio_male,radio_female,radio_admin,radio_user;
      ImageView date;
      Button register;
      TextView LoginBtn;
      FirebaseAuth fAuth;
      FirebaseUser FirebaseUser;
      DatabaseReference reff;
      Employee Employee;
      String currentDateString;
      long maxid=0;
      String user,id;
      Spinner spinner;
      String txtGender = "",txtUserName="", txtRole="";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add_employee);
  
  
          Username = findViewById(R.id.username);
          email = findViewById(R.id.email);
          password = findViewById(R.id.password);
          //dob = findViewById(R.id.dob);
          job = findViewById(R.id.jobtitle);
          department = findViewById(R.id.department);
          mobile = findViewById(R.id.mobile);
  
          radio_male = findViewById(R.id.male);
          radio_female = findViewById(R.id.female);
          radio_admin = findViewById(R.id.Admin);
          radio_user = findViewById(R.id.User);
  
          register = findViewById(R.id.register);
          spinner = findViewById(R.id.spinner);
          //LoginBtn = findViewById(R.id.createText);
          Employee= new Employee();
  
          date = findViewById(R.id.date);
          date.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  DialogFragment datePicker = new DatePickerFragment();
                  datePicker.show(getSupportFragmentManager(), "date picker");
              }
          });
  
          fAuth = FirebaseAuth.getInstance();
  
          reff = FirebaseDatabase.getInstance().getReference("Employee");
  
          reff.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // if (dataSnapshot.exists())
                     // user = (dataSnapshot.getKey());
                  //Log.e("User", user);
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError) {
  
              }
          });
  
  
  
          /*if (fAuth.getCurrentUser() != null){
              startActivity(new Intent(getApplicationContext(),AppStartActivity.class));
              finish();
          }*/
  
          register.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  String txtUserName = Username.getText().toString();
                  String txtEmail = email.getText().toString().trim();
                  String txtPassword = password.getText().toString().trim();
                  String txtMobile = mobile.getText().toString();
                  String txtJob = job.getText().toString().trim();
                  String txtDepatment = department.getText().toString();
                //String txtDob = dob.getText().toString().trim();
                  String job = spinner.getSelectedItem().toString().trim();
  
                  String id = reff.push().getKey();
                  Log.e("id",id);
  
  
                  if(radio_male.isChecked()){
                      txtGender="Male";
                  }
  
                  if(radio_female.isChecked()){
                      txtGender="Female";
                  }
  
                  if(radio_admin.isChecked()){
                      txtRole="1";
                  }
  
                  if(radio_user.isChecked()){
                      txtRole="0";
                  }
  
  
                  //reff.child(String.valueOf(maxid+1)).setValue(Employee);
                 // reff.child(txtUserName).setValue(Employee);
  
                  Toast.makeText(AddEmployee.this,"Data inserted successfully",Toast.LENGTH_LONG).show();
  
  
                  if (TextUtils.isEmpty(txtEmail)){
                      email.setError("Email is Required.");
                      return;
                  }
  
                  if (TextUtils.isEmpty(txtPassword)){
                      password.setError("Password is Required.");
                      return;
  
                  }
  
                  if (password.length() &lt; 6){
  
                      password.setError("Password must be more than 6 characters.");
                      return;
  
                  }
  
                  fAuth.createUserWithEmailAndPassword(txtEmail,txtPassword).addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if (task.isSuccessful()){
  
                              FirebaseUser = fAuth.getCurrentUser();
                              user = FirebaseUser.getUid();
                              //Log.e("id", user);
                              reff.child(user).setValue(Employee);
  
  
  
                              Toast.makeText(AddEmployee.this,"User Created.",Toast.LENGTH_SHORT).show();
                              startActivity(new Intent(getApplicationContext(),AppStartActivity.class));
                          }
  
                          else{
                              Toast.makeText(AddEmployee.this,"Error ! " + task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                          }
  
                      }
                  });
  
  
                  //id = user;
                  //Log.e("id",id);
  
                  Employee.setEmail(txtEmail);
                  Employee.setPassword(txtPassword);
                  Employee.setUsername(txtUserName);
                  Employee.setMobile(txtMobile);
                  Employee.setJob(job);
                  Employee.setDepatment(txtDepatment);
                  Employee.setDob(currentDateString);
                  Employee.setGender(txtGender);
                  Employee.setRole(txtRole);
                  Employee.setId(id);
  
  
              }
          });
  
      }
      @Override
      public void onDateSet(DatePicker view, int year, int month, int dayOfMonth){
          Calendar c = Calendar.getInstance();
          c.set(Calendar.YEAR, year);
          c.set(Calendar.MONTH, month);
          c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  
          currentDateString = DateFormat.getDateInstance(DateFormat.DEFAULT).format(c.getTime());
  
          TextView textView = (TextView) findViewById(R.id.dob);
  
          textView.setText(currentDateString);
  
  
      }
  }
  </code></pre>
  
  <p>And here is my database structure.<a href="https://i.stack.imgur.com/HYu1k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HYu1k.png" alt="enter image description here"></a></p>
  
  <p>Here I want to save the UID in the database which I couldn't get out of the concrete method of the firebase.</p>
  
  <p>Im able to get the UID but not able to save the data, if I give save data in function inside the oncreate its not getting saved. outside the oncreate im numable to save the UID alone. </p>