0

I'm working with fragments and I want to check if my User is registred (on Firebase) or if he's offline (similar to Spotify as you may know)

Anyway, I'm working on the following Code:

FirebaseAuth mAuth;
private DatabaseReference myRef;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private String userID;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
            // Initialize Authentication Object
            mAuth = FirebaseAuth.getInstance();

    if (mAuth.getCurrentUser() != null) {

        View view = inflater.inflate(R.layout.fragment_unregistred, container, true);
        return view;
    } else {
        View view = inflater.inflate(R.layout.fragment_stats, container, true);
        return view;
    }

As you may guess, this gives me an Error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Research on removeView() didn't get me to the point, so I guess this is the wrong way to do it, am I right?

Full Crash Log as requested:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
        at android.view.ViewGroup.addView(ViewGroup.java:4768)
        at android.view.ViewGroup.addView(ViewGroup.java:4708)
        at android.view.ViewGroup.addView(ViewGroup.java:4681)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1434)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
KayD
  • 372
  • 5
  • 17

1 Answers1

0

When fetching details of the current user you need to call the getCurrentUser() method, which is missing in your code. Please try with the snippet down below:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

For additional information and in case you need to look at more options on how to manage user's related information you can check the official documentation

If you need to check the current Network state of the user's device you can have a look at the answers in this thread

I hope that it helps :)

Stoyan
  • 26
  • 5
  • in my Code this is: mAuth = FirebaseAuth.getInstance(); with if (mAuth.getCurrentUser() != null) { .. so it should be the same as you posted – KayD Aug 17 '18 at 14:15
  • It's actually a different statement. Please pay close attention between the code differences and have a look at the provided link to the documentation :) – Stoyan Aug 17 '18 at 15:24