I want to enable an Alert Dialog after user successfully first time login to my app. The Alert Dialog should display at the main page of my app but it display just before proceed to main page. How to display it at the main page of my app ? Below is the code of my app:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE)
{
AccountKitLoginResult result = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
if (result.getError() != null)
{
Toast.makeText(this, ""+result.getError().getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
return;
}
else if (result.wasCancelled())
{
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
return;
}
else
{
if (result.getAccessToken() != null)
{
final AlertDialog waitingDialog = new SpotsDialog(this);
waitingDialog.show();
waitingDialog.setMessage("Please wait...");
waitingDialog.setCancelable(false);
//get current phone
AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
@Override
public void onSuccess(Account account) {
final String userPhone = account.getPhoneNumber().toString();
//check firebase user
users.orderByKey().equalTo(userPhone)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.child(userPhone).exists())
{
//create new user and login
final User newUser = new User();
newUser.setPhone(userPhone);
newUser.setname("");
newUser.setIsstaff("");
//add to firebase
users.child(userPhone).setValue(newUser)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
Toast.makeText(MainActivity.this, "User register successful!", Toast.LENGTH_SHORT).show();
//Login
users.child(userPhone).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User localUser = dataSnapshot.getValue(User.class);
Intent homeIntent = new Intent(MainActivity.this, Home.class);
Common.currentUser = localUser;
startActivity(homeIntent);
waitingDialog.dismiss();
finish();
CompleteProfileNotification();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
else
{
//Login
users.child(userPhone).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User localUser = dataSnapshot.getValue(User.class);
Intent homeIntent = new Intent(MainActivity.this, Home.class);
Common.currentUser = localUser;
startActivity(homeIntent);
waitingDialog.dismiss();
finish();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onError(AccountKitError accountKitError) {
Toast.makeText(MainActivity.this, ""+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
And here is the AlertDialog:
private void CompleteProfileNotification() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Incomplete Profile");
alertDialog.setMessage("Please Add Username and Home Address before ordering.");
LayoutInflater inflater = LayoutInflater.from(this);
View layout_profile = inflater.inflate(R.layout.confirm_signout_layout, null);
alertDialog.setView(layout_profile);
alertDialog.setIcon(R.drawable.ic_person_black_24dp);
alertDialog.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}