Reputation: 135
I have two fragment which are tabbed fragments.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2};
private final Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm, String company) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new GeneralFragment();
break;
case 1:
fragment = new ContactFragment();
break;
}
return fragment;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
@Override
public int getCount() {
return 2;
}
}
ContactFragment is:
public class ContactFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
reference = FirebaseDatabase.getInstance().getReference();
return inflater.inflate(R.layout.fragment_contact, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDataFromDatabase();
}
private void getDataFromDatabase(){
reference.child("mPath1").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String address = dataSnapshot.child("address").getValue().toString();
String email = dataSnapshot.child("e_mail").getValue().toString();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
and GeneralFragment:
public class GeneralFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
reference = FirebaseDatabase.getInstance().getReference();
return inflater.inflate(R.layout.fragment_contact, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDataFromDatabase();
}
private void getDataFromDatabase(){
reference.child("mPath2").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String username = dataSnapshot.child("username").getValue().toString();
String phoneNum = dataSnapshot.child("phone_number").getValue().toString();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError){
}
});
}
}
I want to get address and email when user is seeing the ContactFragment, and get username and phone number when user is seeing GeneralFragment UI. But both of two firebase method run, and all data is being fetched from database when Activity is created. Why this is happening and how to handle this.
Upvotes: 2
Views: 72
Reputation: 6273
The Firebase methods are running at the same time because your SectionsPagerAdapter
loads your two fragments at the same time.
Is there a way you can make one fragment load only when it's needed [in this tabbed scenario]:
No, because the minimum offScreenPageLimit
is 1 and in your case, you have two fragments.
Is there another solution to this?:
Yes.
Since your plan is to execute certain tasks only when the fragment is visible, you can override setUserVisibleHint
in your Fragment like this:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// your Firebase call here
}
}
I hope this helps.
Upvotes: 1