-1

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();
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Liew Syet Chau
  • 143
  • 3
  • 14

2 Answers2

0

Perhaps display the AlertDialog in onCreate() of your main page. Create a SharedPreference value that tracks whether the AlertDialog has been shown previously, and only display it if it hasn't.

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
0

Do this CompleteProfileNotification on the MainActivity instead of Login Screen:

right after onCreate method of MainActivity. All you need to do is track if this is first time you are on MainActivity or not.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(preferences.isFirstLauch()){
            CompleteProfileNotification();
            markFirstLaunch(true);
        }
}

reference: for storing flag in preferences use this https://stackoverflow.com/a/7217834/6236752

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42