I have two fragments in one activity. Based on the flag, which is based on input data entered in fragment1 I should add an item or should not add an item in the second fragment. My problem is condition satisfies for the first time if I go back to the first fragment and change input data. It is not reflected in the second fragment.
Just try this,
Using FragmentPagerAdapter you can show and hide. initially ,
boolean tabUpdate = false;
Set as flag based item count updated function on adapter
@Override
public int getCount() {
if (tabUpdate){
return noOfFragments;
} else {
return noOfFragments - 1;
}
}
Getting changes based on first fragment, need to update adapter and set view pager on next fragment
pagerAdapter.notifyDataSetChanged(); // updated the adapter
mTabLayout.setViewPager(mViewPager);
mViewPager.setCurrentItem("fragment to set on next view");
Add or remove both function need to update the adapter.
You should use the flag in the activity that is hosting both fragments, so when the value is changed from one it should reflect in the other one:
So in the MainActivity:
private bool flag = false;
In the fragment use something like this:
((MainActivity)getActivity()).flag = true
And in the second fragment
if ( ((MainActivity)getActivity()).flag ){
// DO SMETHING
}
I have an app with the same approach and it works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.