user9495575
user9495575

Reputation:

android fragment using onCreateView but cant add firebase

I have created a fragment in my app and I am attempting to add firebase into this fragment. But due to the face that it is using onCreateView it is calling an error on this.

public class ProfileFragment extends Fragment {

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_profile, container, false);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

}

On the mCurrentUser = FirebaseAuth.getInstance().getCurrentUser(); i am getting the error unreachable statement is there any way that i can still use a fragment for this activity?

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view =  inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    ValueEventListener valueEventListener = mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String name = dataSnapshot.child("name").getValue().toString();
            String image = dataSnapshot.child("image").getValue().toString();
            String status = dataSnapshot.child("status").getValue().toString();
            String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    return view;
    });}}

error on return view;

Upvotes: 0

Views: 401

Answers (4)

Faiizii Awan
Faiizii Awan

Reputation: 1710

Update this

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
}

To

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_profile, container, false);
    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    String current_uid = Objects.requireNonNull(mCurrentUser).getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

    ValueEventListener valueEventListener = mUserDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String name = dataSnapshot.child("name").getValue().toString();
        String image = dataSnapshot.child("image").getValue().toString();
        String status = dataSnapshot.child("status").getValue().toString();
        String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }

    // you are calling return view inside the Value Listner that why you are getting the error
    });
    return view;
}

Upvotes: 1

olajide
olajide

Reputation: 967

Let onActivityCreated() method handles additional job.

ValueEventListener is an inner class of DatabaseReference, hence, you don't necessary need to create object before instantiating it. So, remove the line.

Also, be reminded that return view; should be the last statement in your onCreateView(). Make sure that before closing it you have return statement.

Now do this. After onCreateView(), create another copy and past the below code.

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



   mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String name = dataSnapshot.child("name").getValue().toString();
            String image = dataSnapshot.child("image").getValue().toString();
            String status = dataSnapshot.child("status").getValue().toString();
            String thumb_image = 
             dataSnapshot.child("thumb_image").getValue().toString();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        });
}

Upvotes: 0

Qasim
Qasim

Reputation: 5321

Move the below line before return statment in onCreateView() method.

 mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

Anything after return statement is unreachable, as error statement states.

Upvotes: 0

Łukasz Kobyliński
Łukasz Kobyliński

Reputation: 1111

Just move mCurrentUser = FirebaseAuth.getInstance().getCurrentUser() line before return statement.

Upvotes: 0

Related Questions