title: Sending user to MainActivity after database check with Firebase error
body: <p>After LoginActivity my app proceeds to SetupActivity in which user needs to input his email and there is a save button which uploads the email to firebase and then it should proceed to MainActivity and this should happen only once for an user. <strong>The problem is save button stores the email to the database but doesn't proceed to mainActivity and setupActivity shows again.</strong></p>
  
  <p>SetupActivity.java</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  
  import android.content.Intent;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  
  import java.util.HashMap;
  
  public class SetupActivity extends AppCompatActivity {
  
      private EditText mUserEmail, mUserAge;
      private Button mSaveSetupButton;
  
      private FirebaseAuth mAuth;
      private DatabaseReference UsersRef;
  
      String currentUserId;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_setup);
  
          mAuth = FirebaseAuth.getInstance();
  
          currentUserId = mAuth.getCurrentUser().getUid();
          UsersRef = FirebaseDatabase.getInstance().getReference().child(currentUserId);
          mUserEmail = findViewById(R.id.userEmailInput);
          mUserAge = findViewById(R.id.userAgeInput);
          mSaveSetupButton = findViewById(R.id.saveSetupButton);
  
          mSaveSetupButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  SaveAccountSetupInformation();
              }
          });
      }
  
      private void SaveAccountSetupInformation()
      {
          String useremail = mUserEmail.getText().toString();
          String userage = mUserAge.getText().toString();
  
          if (TextUtils.isEmpty(useremail)) {
              Toast.makeText(this, "Please enter your email.", Toast.LENGTH_SHORT).show();
          }
          if (TextUtils.isEmpty(userage)) {
              Toast.makeText(this, "Please write your age.", Toast.LENGTH_SHORT).show();
          }
          else
          {
              //you can put some progress bar here
  
              HashMap&lt;String, Object&gt; userMap = new HashMap&lt;String, Object&gt;();
              userMap.put("email", useremail);
              userMap.put("age", userage);
              userMap.put("gender", "");
              userMap.put("loves", "0");
              userMap.put("stars", "0");
              UsersRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
                  @Override
                  public void onComplete(@NonNull Task task)
                  {
                      if(task.isSuccessful())
                      {
                          SendUserToMainActivity();
                      }
                      else
                      {
                          String errormessage =  task.getException().getMessage();
                          Toast.makeText(SetupActivity.this, "Error Occured: " + errormessage, Toast.LENGTH_SHORT).show();
                      }
                  }
              });
          }
  
      }
  
      private void SendUserToMainActivity() {
          Intent MainIntent = new Intent(SetupActivity.this, MainActivity.class);
          MainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(MainIntent);
          finish();
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>MainActivity.java</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import android.content.Intent;
  import android.support.annotation.NonNull;
  import android.support.design.widget.BottomNavigationView;
  import android.support.design.widget.NavigationView;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.Toolbar;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.FrameLayout;
  import android.widget.Toast;
  
  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;
  
  public class MainActivity extends AppCompatActivity {
  
      private BottomNavigationView mMainBottomNav;
      private FrameLayout mMainFrame;
      private ActionBarDrawerToggle actionBarDrawerToggle;
  
      private HomeFragment homeFragment;
      private NotificationsFragment notificationsFragment;
      private MessagesFragment messagesFragment;
  
      private NavigationView navigationView;
      private DrawerLayout drawerLayout;
      private RecyclerView postList;
      private Toolbar mToolbar;
  
      private FirebaseAuth mAuth;
      private DatabaseReference UsersRef;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mAuth = FirebaseAuth.getInstance();
          UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
  
          navigationView = findViewById(R.id.navigation_view);
          View navView = navigationView.inflateHeaderView(R.layout.navigation_header);
  
          mMainFrame = findViewById(R.id.main_container);
          mMainBottomNav = findViewById(R.id.main_bottom_nav);
  
          //Toolbar
          mToolbar = findViewById(R.id.main_page_toolbar);
          setSupportActionBar(mToolbar);
          getSupportActionBar().setTitle("Home");
  
          homeFragment = new HomeFragment();
          notificationsFragment = new NotificationsFragment();
          messagesFragment = new MessagesFragment();
  
          // EDITED
          // setFragment(homeFragment);
  
          drawerLayout = findViewById(R.id.drawable_layout);
          actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
          drawerLayout.addDrawerListener(actionBarDrawerToggle);
          actionBarDrawerToggle.syncState();
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
          {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
              {
                  UserMenuSelector(menuItem);
                  return false;
              }
          });
          mMainBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
          {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
              {
  
                  switch (menuItem.getItemId())
                  {
                      case R.id.nav_home :
                          setFragment(homeFragment);
                          return true;
  
                      case R.id.nav_notif :
                          setFragment(notificationsFragment);
                          return true;
  
                      case R.id.nav_messages :
                          setFragment(messagesFragment);
                          return true;
  
                      default :
                          return false;
                  }
              }
          });
  
      }
  
      @Override
      protected void onStart()
      {
          super.onStart();
          //Check the user if they are login or not
          FirebaseUser currentUser =  mAuth.getCurrentUser();
  
          if(currentUser == null)
          {
              SendUserToLandingActivity();
          }
          else
          {
              CheckUserExistence();
          }
      }
  
      private void CheckUserExistence()
      {
          final String current_user_id = mAuth.getCurrentUser().getUid();
  
          UsersRef.addValueEventListener(new ValueEventListener()
          {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot)
              {
                  if(!dataSnapshot.hasChild(current_user_id))
                  {
                      SendUserToSetUpActivity();
                  }
                  else
                  {
                  }
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError)
              {
              }
          });
      }
  
      private void SendUserToMainActivity()
      {
          Intent MainIntent = new Intent(MainActivity.this, MainActivity.class);
          MainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(MainIntent);
          finish();
      }
  
      private void SendUserToSetUpActivity()
      {
          Intent SetupIntent = new Intent(MainActivity.this, SetupActivity.class);
          SetupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(SetupIntent);
          finish();
      }
  
      private void SendUserToLandingActivity()
      {
          Intent loginIntent = new Intent(MainActivity.this, LandingActivity.class);
          loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(loginIntent);
          finish();
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      {
          if(actionBarDrawerToggle.onOptionsItemSelected(item))
          {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      private void UserMenuSelector(MenuItem menuItem)
      {
          switch (menuItem.getItemId())
          {
              case R.id.nav_profile:
                  Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_bookmarks:
                  Toast.makeText(this, "Bookmarks", Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_settings:
                  Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_about:
                  Toast.makeText(this, "About", Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_logout:
                  mAuth.signOut();
                  SendUserToLandingActivity();
                  break;
          }
      }
  
      private void setFragment(Fragment fragment)
      {
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.main_container, fragment);
          fragmentTransaction.commit();
      }
  }</code></pre>
  </div>
  </div>
  </p>