Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
Dari SetupActivity ke MainActivity
Jadi aplikasi saya ini setelah LoginActivity setelah Login sukses dikirim ke MainActivity dan jika di cek user tsb di database sudah terdaftar tapi belum melakukan pengisisan data maka akan di kirim ke SetupActivity setelah melakukan pengisian data seperti email dan umur secara sukses maka akan dikirimkan kembali ke MainActivity dan login selanjutnya tidak perlu SetupActivity lagi.
Tapi disini masalah nya SetupActivity nya terus muncul jadi setelah tekan tombol save, datanya terkirim secara sukses ke database Firebase nya, tapi layar SetupActivity nya berupa pengisian data email dan umur terus muncul. apa karna ada masalah di pengecekan database atau script nya salah di SetupActivity atau MainActivity ya? Mohon Bantuan nya saya memiliki pengalaman 0 hanya sekedar suka saja belajar2 sedikit.
MainActivity.java
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;
}
}
}
SetupActivity.java
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<String, Object> userMap = new HashMap<String, Object>();
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();
}
}
3 Jawaban:
di method "CheckUserExistence" di onDataChange disitu ada kondisi jika !dataSnapshot.hasChild(current_user_id) maka akan di alihkan ke SetupActivity.
Berarti disitu yang dicek kan apakah ada current_user_id tersebut di database. Jika tidak ada, suruh dia isi email, age, gender, dll di SetupActivity.
Kenapa gk gini aja kondisinya jika !dataSnaptot.current_user_id.hasChild(gender/email/age, dll) berarti kalau id nya ada tetapi gender, email, age dll tidak ada baru dia dialihkan ke setup user/SetupActivity. Kalau sudah ada gk usah dialihkan.
Mohon maaf kalau saya, saya kurang lancar juga memakai firebase hehehehe
Tanggapan
Terimakasih atas jawaban nya tapi tetap belum menyelesaikan masalah ini
owh iya, sekedar info di method mu "SendUserToMainActivity" ada Intent/MainIntent dari MainActivity menuju MainActivity Biasa nya Intent dari satu activity ke activity lain.
Tanggapan
Kalo ini saya sengaja buat iseng karna mumet bgt pikiran saya gimana cara nya biar user gak balik ke SetupActivity lupa saya hapus maaf memalukan hehe
I Solved it
Jadi saya lupa menambahkan .child(); lagi setelah .child(currentUserId) di SetupActivity
Jadi saat di MainActivity database nya membaca Child dari "Users" tapi saat di SetupActivity data yg dikirimkan ke database tanpa parent "Users" jadi langsung currentUserId nya makanya error
ini versi bener nya.
<pre> UsersRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId); </pre>