Following this example I'm trying to replace one fragement with another within a single tab of a ViewPager. I'm getting this exception:
java.lang.IllegalStateException: Can't change tag of fragment DailyWordFragment
class ViewPagerAdapter extends FragmentPagerAdapter {
private final FragmentManager fragmentManager;
Fragment fragment;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
fragmentManager = manager;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
if(fragment == null){
fragment = DailyWordFragment.newInstance();
}
return fragment;
case 1:
if(fragment == null){
fragment = WordListFragment.newInstance(new WordListFragment.OnWordItemAddListener() {
#Override
public void onWordItemAddListener() {
fragmentManager.beginTransaction().remove(fragment).commit();
fragment = AddWordFragment.newInstance();
notifyDataSetChanged();
}
});
}
return fragment;
default:
return null;
}
}
#Override
public int getItemPosition(Object object) {
if(object instanceof WordListFragment && fragment instanceof AddWordFragment)
return POSITION_NONE;
return POSITION_UNCHANGED;
}
#Override
public int getCount() {
return 2;
}
}
According to other posts this exception seems to be caused by adding the same fragment repeatedly, or getting miscounting the number of fragments but I can't see where either of these are occurring if at all? Clearly I'm missing something. I would appreciate any help. Thank you.
Have you tried before you initialize the Fragment to set it to null?
#Override
public Fragment getItem(int position) {
fragment = null
switch (position){
case 0:
if(fragment == null){
fragment = DailyWordFragment.newInstance();
}
return fragment;
case 1:
if(fragment == null){
fragment = WordListFragment.newInstance(new WordListFragment.OnWordItemAddListener() {
#Override
public void onWordItemAddListener() {
fragmentManager.beginTransaction().remove(fragment).commit();
fragment = AddWordFragment.newInstance();
notifyDataSetChanged();
}
});
}
return fragment;
default:
return null;
}
}
To SWAP fragments in one tab of a View Pager you have to do it in a different way:
You need a fragment that goes in the ViewPager that acts as a container. which is managed by the ViewPager. This Fragment is allways there in the tab, never swaps or changes.
Inside that Fragment you have 2 child fragments that you can swap using transactions with the ChildFragmentManager. But the ViewPager doesn't even know about their existance.
Related
I have problem with fragments in ViewPager. In my app I have 4 tabs implemented with FragmentStatePagerAdapter.
Here is my SectionsPageAdapter.java
public class SectionsPageAdapter extends FragmentStatePagerAdapter {
private String fragment0 = "";
private Fragment fragAt0;
private FragmentManager manager;
private String[] tabNames;
Context context;
public SectionsPageAdapter(FragmentManager fm, Context ctx, String lastFrag) {
super(fm);
context = ctx;
tabNames = ctx.getResources().getStringArray(R.array.tabs_name);
this.manager = fm;
this.listener = new fragmentChangeListener();
fragment0 = lastFrag;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ActivityTypeFragment.newInstance(0);
case 1:
return EventsFragment.newInstance();
case 2:
return CalendarFragment.newInstance();
case 3:
if(fragAt0 == null){
switch(fragment0){
case "Chart":
fragAt0 = ChartFragment.newInstance(listener);
break;
case "Hour":
fragAt0 = HourChartFragment.newInstance(listener);
break;
case "Daily":
fragAt0 = DailyChartFragment.newInstance(listener);
break;
default:
fragAt0 = ChartFragment.newInstance(listener);
}
}
return fragAt0;
default:
return ActivityTypeFragment.newInstance(0);
}
}
#Override
public int getCount() {
return 4;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
#Override
public int getItemPosition(#NonNull Object object) {
if (object instanceof ChartFragment && fragAt0 instanceof DailyChartFragment
|| object instanceof HourChartFragment && fragAt0 instanceof DailyChartFragment)
{
return POSITION_NONE;
}
else if (object instanceof DailyChartFragment && fragAt0 instanceof ChartFragment
|| object instanceof HourChartFragment && fragAt0 instanceof ChartFragment){
return POSITION_NONE;
}
else if (object instanceof HourChartFragment && fragAt0 instanceof ChartFragment)
{
return POSITION_NONE;
}
else if (object instanceof ChartFragment && fragAt0 instanceof HourChartFragment
|| object instanceof DailyChartFragment && fragAt0 instanceof HourChartFragment)
{
return POSITION_NONE;
}
return POSITION_NONE;
}
public interface nextFragmentListener {
void fragment0Changed(String newFragmentIdentification, FragmentManager fragmentManager);
}
private nextFragmentListener listener;
private final class fragmentChangeListener implements nextFragmentListener {
#Override
public void fragment0Changed(String fragment, FragmentManager fm) {
fragment0 = fragment;
manager.beginTransaction().remove(fragAt0).commitNow();
switch (fragment) {
case "Chart":
fragAt0 = ChartFragment.newInstance(listener);
break;
case "Daily":
fragAt0 = DailyChartFragment.newInstance(listener);
break;
case "Hour":
fragAt0 = HourChartFragment.newInstance(listener);
break;
}
SectionsPageAdapter.this.notifyDataSetChanged();
}
}
public String getFragmentName()
{
return fragment0;
}
}
So in last tab I have a fragment - ChartFragment, where I have Spinner with options to select another chart. And when user selects another chart, this another chart reloads in this tab by calling method fragment0Changed that I have in my SectionsPageAdapter
Class. I found this solution in https://stackoverflow.com/a/27304530/1573414.
Here is my function onItemSelected which I have in every fragment that I want to be appeared in Charts tab
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (view != null) {
String selectedMenu = ((TextView) view).getText().toString();
if (selectedMenu == getString(R.string.best_appealing_chart)) {
LoadBestAppealingChart();
} else if (selectedMenu == getString(R.string.hour_chart)) {
chartListener.fragment0Changed("Hour", getActivity().getSupportFragmentManager());
}
else
{
chartListener.fragment0Changed("Daily", getActivity().getSupportFragmentManager());
}
}
else {
LoadBestAppealingChart();
}
}
So, everything works fine until I rotate device, after that selecting another chart does not work. App throwing the exception:
java.lang.IllegalStateException: Fragment host has been destroyed
at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2183)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2211)
at android.support.v4.app.BackStackRecord.commitNow(BackStackRecord.java:643).
I’ve read the article https://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html,but in my case I have nor asynchronus methods where I would replace a fragment, nor I call it in Activity lifecycle methods. I make a fragment transaction in a onSelectionChanged method of my spinner. What I tried: using commitNowAllowingStateLoss() do not make things better, it just didn’t throw an exception, fragments still do not reloading; passing getSupportFragmentManager() parameter to fragment0Changed method doesn’t help either, but doing this does not throw any exception even if i use simple commitNow() method. But what helps is when after rotation I choose first or second tab of my app then go to my fourth tab(Charts tab), then I can switch(reload) my fragments without any problem. Interesting thing that switching to third tab(Calendar tab) does not help either. After switching to third tab and returning to fourth tab switching fragments still do not reloaded.
I think that its a common mistake on Android to communicate with Fragment passing listener. Listener on Fragment must be implemented by the Activity or a child Fragment. So you have to make your Activity or Fragment implements NextFragmentListener... Second, never make a reference to a Fragment on ViewPager, you are leaking the old fragment0
public class SubAdapter extends FragmentStatePagerAdapter
{
/* just pass the behavior in super() call so every time resume method will call so that you can reload any thing you want
But you should use subclass extending */
private String tile_list[];
public SubAdapter (FragmentManager fm, String[] tile_list)
{
super(fm,FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.tile_list = tile_list;
}
#Override
public Fragment getItem(int position)
{
Fragment fragment;
switch (position)
{
case 0:
fragment=new new_fragment();
break;
default:
fragment=new new_fragment();
}
return fragment;
}
#Override
public int getCount()
{
return tile_list.length;
}
#Nullable
#Override
public CharSequence getPageTitle(int position)
{
return tile_list[position];
}
}
I'm using fragmentpageradapter to inflate fragment , this is my coding.
i'm adding cart item in one fragment and showing it to other fragment and all fragment comes in tab layout.My problem is cart item is not refreshing on other fragment while swiping fragment.
public class ProfileDetailePagerAdapter extends FragmentPagerAdapter
{
int TabCount;
HashMap<Integer, String> mFragmentTags;
FragmentManager mFragmentManager;
private boolean isPagingEnabled = true;
public ProfileDetailePagerAdapter(FragmentManager fm, int TabCount) {
super(fm);
this.TabCount = TabCount;
mFragmentTags = new HashMap<Integer, String>();
mFragmentManager = fm;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
MenuFragment tab1 = new MenuFragment();
return tab1;
case 1:
SelectMember_fragment tab2 = new SelectMember_fragment();
return tab2;
case 2:
ViewCart_fragment tab3 = new ViewCart_fragment();
return tab3;
/* case 3:
PaymentMode_fragment tab4=new PaymentMode_fragment();
return tab4;*/
}
return null;
}
#Override
public int getCount() {
return TabCount;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
// record the fragment tag here.
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
}
return obj;
}
public Fragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return mFragmentManager.findFragmentByTag(tag);
}
}
Call FragmentStatePagerAdapter
You should use addOnTabSelectedListener
This method is used to add a listener that will be invoked when tab
selection changes.
tabLayoutOBJ.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
public void onTabReselected(TabLayout.Tab tab) {
}
public void onTabSelected(TabLayout.Tab tab){
String getTAB = (String)tab.getText();
// Do your WORK
}
public void onTabUnselected(TabLayout.Tab tab){
}
});
The reason behind it is the implementation of viewpager. There is a property called offset in ViewPager to make ready fragments next to it up to offset(int) value.
Default value is 1. For example, you are in 1st position, 0th and 2nd positions are also created.
On the other hand if you are in 0th position 1st is the only created fragment. If you swipe to right (going 1st) then 2nd is going to be created.
I assume you are changing a shared object from 1st fragment and want to notify 2nd fragment with changed data. To solve it you need to use listener. The elegant way to solve it is adding observer pattern to your shared object.
Good luck
Emre
Like in the image below, I have an Activity with a ViewPager (with TabLayout) inside.
Now I want to refresh for example the text of an TextView in a ViewPager Fragment. How can I do this?
http://imgur.com/0kWQCfx
[Sorry for the image link, but I don't have enough reputations yet.]
I already tried something like this:
//In Activity
View root = getLayoutInflater().inflate(R.layout.fragment_home, null);
TextView textView = (TextView) root.findViewById(R.id.text_view);
textView.setText("test");
But I get a NullPointerException for the textView.
Edit 1:
I only need to access the components inside one Fragment, so I am not using an array inside the Adapter.
I changed my ViewPagerAdapter to this:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public HomeFragment fragment;
...
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment = new HomeFragment();
case 1:
...
default:
return null;
}
}
But I still get a NullPointerException here:
final Fragment root = viewPagerAdapter.fragment;
Log.d("testtest", String.valueOf(root == null));
Write this code in your activity where you want to change fragment value. Your fragment id is e.g. my_fragment
Fragment frag = getFragmentManager().findFragmentById(R.id.my_fragment)
((TextView) frag.getView().findViewById(R.id.text_view)).setText(s);
You can create your own Adapter:
private class YourAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener
{
Fragment screens[];
public CheckinHistoryAdapter(FragmentManager fm) {
super(fm);
screens = new Fragment[3];
screens[0] = new CoachFragment();
screens[1] = new LogingFragment();
screens[2] = new HistoryFragment();
}
#Override
public Fragment getItem(int index) {
if(index <= screens.length)
{
return screens[index];
}
return null;
}
#Override
public int getCount() {
return screens.length;
}
}
Use it like this
mViewPager.setAdapter(this.mPagerAdapter);
You can access fragment object with getItem method.
If you only need a reference to one Fragment, then just use the position parameter passed into instantiateItem(), and only assign your Fragment reference for the desired position:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public HomeFragment fragment;
//...
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HomeFragment(); //modified
case 1:
...
default:
return null;
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
if (position == 0) {
fragment = (HomeFragment) createdFragment;
}
return createdFragment;
}
}
In my application I have a View pager that rotates 4 Fragments. Some of those fragments have other fragments inside them. The first time Viewpager starts a Fragment it works fine, but if I rotate around and return to the previous fragment, the inner fragments fail to show ( and I have noticed that getActivity() in them returns null).
For example, the app starts, I move through viewpager and see all the main fragments and they appear fine, then I scroll back, every main fragment that has inner fragments does not show properly.
I use "FragmentStatePagerAdapter" for my ViewPager and the code is below (variables names are for example).
private class ContentPagerAdapter extends FragmentStatePagerAdapter {
public ContentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public Fragment getItem(int pos) {
Fragment fragment = null;
try {
if (pos == POS1) {
if (fragment1 == null) {
fragment1 = Fragment1.newInstance();
}
fragment = fragment1;
} else if (pos == POS2) {
if (fragment2 == null) {
fragment2 = Fragment2.newInstance(something);
}
fragment = fragment2;
} else if (pos == POS3) {
if ( fragment3 == null ) {
fragment3 = Fragment3.newInstance(something3);
}
fragment = fragment3;
} else { //default
if (fragment4 == null) {
fragment4 = Fragment4.newInstance(something4);
}
fragment = fragment4;
}
}catch (Exception e) {
Log.e(AppConstants.APP_TAG, "Cannot create fragment", e);
finish();
}
return fragment;
}
#Override
public int getCount() {
int count = 4;
return count;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
When instantiating the PagerAdapter, use getChildFragmentManager() instead of getFragmentManager(). If more info needed, similiar problem was solved here ViewPager in TabFragment not loading second time
I am using FragmentPagerAdapter and I need to refresh the content after user action.
To do this, I am calling notifyDataSetChanged() on the adapter but nothing seems happen. I would like to avoid using FragmentStatePagerAdapter, since it contains just 3 pages.
Here is the override methods of the adapter:
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case PAGE_HOME:
fragment = HomeFragment.newInstance();
break;
case PAGE_CATEGORIES:
fragment = CategoriesFragment.newInstance();
break;
case PAGE_ACCOUNT:
if (account == null) {
fragment = AccountFragment.newInstance();
} else {
fragment = MyAccountFragment.newInstance();
}
break;
default:
fragment = null;
}
pageReferenceMap.put(position, fragment);
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
pageReferenceMap.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
pageReferenceMap.remove(position);
}
Thanks.
I found out that I have to return a different id for that page position. So here is how I return a unique id based on the fragment I want to display and not based on the page position:
#Override
public long getItemId(int position) {
long id;
switch (position) {
case HOME_PAGE_POSITION:
//fall through
case CATEGORIES_PAGE_POSITION:
id = position;
break;
case ACCOUNT_PAGE_POSITION:
if (account == null) {
id = position;
} else {
id = position + PAGE_COUNT;
}
break;
default:
id = -1;
}
return id;
}
If you have more that 2 fragments per pages to display at a time, you can just return position+PAGE_COUNT*n. Pretty easy.
Hope this help.
notifyDataSetChanged method will send a call back to your adapter , and check for the getCount method following this call back the getItem method is called. Your content in the screen refresh only if the content reflect any change in the currently selected fragment.The point to note here is that This callback will not select your first fragment. It will stay in the currently selected fragment.
For example:-
The currently selected fragment is 2, and you have changed the contents of fragment number 1 and makes a notifyDataSetChanged callback, so in this case you will not see any change as fragment 2 is currently present in the screen.