Я пытаюсь отправить данные из моей Activity (которая получает данные из Firebase) в другие фрагменты. Он работает только в том случае, если я изменяю данные на Firebase, но не на его первой попытке, пока действие создано. С первой попытки фрагменты в методах passDataToInfoFragment и passDataToRanklistFragment всегда имеют значение NULL. Я понятия не имею, почему, поэтому мне нужна помощь ур.Почему мой фрагмент не создан? Activity to Fragments communication
GroupDetailFragmentCommunication:
public interface GroupDetailFragmentCommunication {
public void passDataToInfoFragment(PublicGroup group);
public void passDataToRanklistFragment(PublicGroup group);
}
GroupDetailActivity:
public class GroupDetailActivity extends AppCompatActivity implements GroupDetailFragmentCommunication{
private FirebaseAuth auth;
private FirebaseDatabase database;
private DatabaseReference reference;
private GroupDetailPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_detail);
this.initFirebase();
this.setUpToolbar();
this.setUpTabLayout();
this.getPublicGroup();
}
public void initFirebase() {
auth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
reference = database.getReference();
}
public void setUpToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Details");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void setUpTabLayout() {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ArrayList<String> tabs = new ArrayList<>();
tabs.add("Info");
tabs.add("Rank");
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
this.adapter = new GroupDetailPagerAdapter(getSupportFragmentManager(), tabs);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
public void getPublicGroup() {
reference.child("groups").child("public").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
PublicGroup pg = snapshot.getValue(PublicGroup.class);
if (pg != null) {
pg.setKey(snapshot.getKey());
passDataToInfoFragment(pg);
passDataToRanklistFragment(pg);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void passDataToInfoFragment(PublicGroup group) {
InfoFragment fragment = (InfoFragment) this.adapter.getFragment(0);
if (fragment != null) {
fragment.fragmentCommunication(group);
} else {
Log.i("Bla", "Fragment 1 is not instantiated");
}
}
s
@Override
public void passDataToRanklistFragment(PublicGroup group) {
RanklistFragment fragment = (RanklistFragment) this.adapter.getFragment(1);
if (fragment != null) {
fragment.fragmentCommunication(group);
} else {
Log.i("Bla", "Fragment 2 is not instantiated");
}
}
}
GroupDetailPagerAdapter:
public class GroupDetailPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private ArrayList<String> tabHeader;
public GroupDetailPagerAdapter(FragmentManager fm, ArrayList<String> tabHeader) {
super(fm);
this.tabHeader = tabHeader;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment infoTab = new InfoFragment();
return infoTab;
case 1:
Fragment ranklistTab = new RanklistFragment();
return ranklistTab;
default:
return null;
}
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getFragment(int position) {
return registeredFragments.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return tabHeader.get(position);
}
}
Thx для ур помочь, теперь он работает! –