1

Here is my onStart method I want to open an activity when user has updated his profile name

@Override
protected void onStart() {
    super.onStart();
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser != null) {
        if (firebaseUser.getDisplayName() == null) {
            startActivity(new Intent(WorkerMaps.this, FirstWorkerInformation.class));
            finish();
        }
    }

}
Amit Ghosh
  • 63
  • 6
  • 1
    Although, it says name is not null as you're already checking `FirebaseUser` for null and thus, the default null-checking mechanism perceives it like if `FirebaseUser` is not null, it's entities won't be null too but that't not the actual case because many a time, `DisplayName()` return null too. So, ignore that suggestion, that's how the default mechanism works. – Lalit Fauzdar May 25 '20 at 15:33
  • I replace the if (firebaseUser!=null), still it doesn't work – Amit Ghosh May 25 '20 at 15:42
  • Sorry sir I can't ignore that as my interface depends upon whether the user has provided his name or not – Amit Ghosh May 25 '20 at 15:44
  • What doesn't work? You didn't have to replace anything, it was perfectly fine, just ignore that warning/suggestion. – Lalit Fauzdar May 25 '20 at 15:44
  • 1
    By ignoring, I meant that you've to ignore that suggestion which says `(if getCurrentUser.getDisplayname != null) is always true`. Leave that null check there as it is, Null cheking is very important. – Lalit Fauzdar May 25 '20 at 15:46
  • No, actually there is no suggestion, the case is when i update my display name it returns me the name all good till now but when there is no display name still it's returning some values like " " (a space). – Amit Ghosh May 25 '20 at 15:49
  • Please help it's like my nightmare – Amit Ghosh May 25 '20 at 15:51

1 Answers1

1

First, nowhere in the question have you mentioned your actual problem.

Hence, what your problem is that even if the displayName() doesn't contain a name, it still returns something which is not null.

Hence, what you can do for that is modify your code and use isBlank() as this.

if (firebaseUser.getDisplayName() == null || firebaseUser.getDisplayName().isBlank()) {

Now, what this will do that it will also check if your displayName only containts spaces and no characters and if true, it will enter in your if scope.

You can read more about isBlank() here.

Update: Because isBlank() is a part of StringUtils and that's a separate library provided by Apache, You can import that library as answered here.

But if you don't want to use a separate library, you can also use trim() which removes spaces from a string's starting and ending point as:
" Name " -> "Name"
" " -> ""

So, what you can do with it, you can check its length and if it's 0, then it will return true as in case of " ". You can do it as:

if (firebaseUser.getDisplayName() == null || firebaseUser.getDisplayName().trim().length() == 0){

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • there is no method as isBlank() showing but isEmpty() is showing instead – Amit Ghosh May 25 '20 at 16:24
  • Ah, Because it's a part of `StringUtils` and you've to use import it as done [here](https://stackoverflow.com/a/26773506/8244632). This is a separate lightweight library and although, it will provide many extra features but if you don't want to use a library, you can also use trim() which removes space from starting and ending of a string. Use it as `firebaseUser.getDisplayName().trim().length() == 0`. I'll also update this answer as well. – Lalit Fauzdar May 25 '20 at 16:39
  • Hey @AmitGhosh try now. I've updated it. – Lalit Fauzdar May 25 '20 at 16:45
  • Still not working – Amit Ghosh May 26 '20 at 05:45
  • 1
    Thanks for answering it worked for me – Amit Ghosh May 29 '20 at 15:21
  • forgive me considering a newbie – Amit Ghosh May 29 '20 at 15:35
  • 1
    @AmitGhosh I'm glad it helped. And there's nothing you should be forgiven for, everyone was a newbie once. You can personally message me if you don't get answer of something, I'll try to help. – Lalit Fauzdar May 29 '20 at 16:09