Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. public class RegisterActivity extends AppCompatActivity{
  2.  
  3. private EditText mNickname;
  4. private EditText mEmail;
  5. private EditText mPassword;
  6. private TextView gotoLoginPage;
  7. private Button mRegisterbutton;
  8. private FirebaseAuth mAuth;
  9. private ProgressDialog mRegProgress;
  10.  
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_register);
  16. mNickname = findViewById(R.id.registerNicknameId);
  17. mEmail = findViewById(R.id.registerEmailId);
  18. mPassword = findViewById(R.id.registerPasswordId);
  19. FirebaseApp.initializeApp(this);
  20.  
  21.  
  22. mRegisterbutton = findViewById(R.id.registerButton);
  23. gotoLoginPage = findViewById(R.id.gotoLoginPage);
  24.  
  25. getSupportActionBar().setTitle("Create new account");
  26. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  27.  
  28. mRegProgress = new ProgressDialog(this);
  29.  
  30. mRegisterbutton.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. String nickname = mNickname.getText().toString();
  34. String email = mEmail.getText().toString();
  35. String password = mPassword.getText().toString();
  36.  
  37. if (mNickname.equals("")|| mEmail.equals("") || mPassword.equals("")){
  38. Toast.makeText(getApplicationContext(), "Fill in the fields", Toast.LENGTH_LONG).show();
  39. mRegProgress.show();
  40.  
  41. registerUser(email, password);
  42. }
  43. Intent toHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
  44. startActivity(toHomePage);
  45. finish();
  46. }
  47. });
  48.  
  49. gotoLoginPage.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. Intent tologinPage = new Intent(RegisterActivity.this, LoginActivity.class);
  53. startActivity(tologinPage);
  54. }
  55. });
  56.  
  57. }
  58.  
  59. private void registerUser(String email, String password) {
  60. mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  61. @Override
  62. public void onComplete(@NonNull Task<AuthResult> task) {
  63. if (task.isSuccessful()){
  64. Intent gotoHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
  65. startActivity(gotoHomePage);
  66. finish();
  67. }
  68. }
  69. });
  70. }
  71.  
  72. public class HomeActivity extends AppCompatActivity {
  73.  
  74. private FirebaseAuth mAuth;
  75. private Toolbar mToolbar;
  76.  
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState) {
  79. super.onCreate(savedInstanceState);
  80. setContentView(R.layout.activity_home);
  81.  
  82. FirebaseApp.initializeApp(this);
  83.  
  84. mToolbar =findViewById(R.id.toolbarId);
  85. getSupportActionBar().setTitle("PhorumChat");
  86. }
  87.  
  88.  
  89. private void sendToStart() {
  90.  
  91. Intent goToWelcomePage = new Intent(HomeActivity.this, WelcomeActivity.class);
  92. startActivity(goToWelcomePage);
  93. finish();
  94. }
  95.  
  96. @Override
  97. public boolean onCreateOptionsMenu(Menu menu) {
  98. super.onCreateOptionsMenu(menu);
  99. getMenuInflater().inflate(R.menu.main_menu, menu);
  100.  
  101. return true;
  102. }
  103.  
  104. @Override
  105. public boolean onOptionsItemSelected(MenuItem item) {
  106. super.onOptionsItemSelected(item);
  107.  
  108. if (item.getItemId() == R.id.main_logoutBtn){
  109. FirebaseAuth.getInstance().signOut();
  110. sendToStart();
  111. }
  112.  
  113. return true;
  114. }
  115.  
  116. @Override
  117. public void onStart() {
  118. super.onStart();
  119. // Check if user is signed in (non-null) and update UI accordingly.
  120. FirebaseUser currentUser = mAuth.getCurrentUser();
  121. updateUI(currentUser);
  122. }
  123.  
  124. private void updateUI(FirebaseUser currentUser) {
  125. if (currentUser ==null){
  126. Intent homepage= new Intent(HomeActivity.this, WelcomeActivity.class);
  127. startActivity(homepage);
  128. finish();
  129. }
  130. }
  131.  
  132. public void onStart() {
  133. super.onStart();
  134. // Check if user is signed in (non-null) and update UI accordingly.
  135. FirebaseUser currentUser = mAuth.getCurrentUser();
  136. updateUI(currentUser);
  137. }
  138.  
  139. mAuth = FirebaseAuth.getInstance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement