-1

I have a similar problem to the one described here: ViewPager only displaying last fragment, but after attempting the solution there I'm still having the same issue.

I have an app with a TabLayout, which displays 3 fragments. The 3 fragments have the same layout, but the content (text, etc...) of said layouts change depending on the position in the TabLayout. Tab 1 displays one thing, tab 2 another, etc. I do this by passing one class or another to the fragment to be displayed depending on the position passed to getItem() in the pager adapter.

What I'm seeing is that no matter what fragments I attempt to load, my tab layout will always display 3 instances of the same fragment, in this case the last one that gets instantiated. I've stepped through the code and sure enough 3 different fragments get instantiated and returned in getItem() the 3 times its called, but only the one that is returned last is set onto the TabLayout, 3 times.

How can I get it to display the 3 distinct fragments, and not the same one 3 times?

Code used:

@Override
public Fragment getItem(int position) {

    MyFragment fragment = new MyFragment();

    if (position == 0) {
        fragment = MyFragment.newInstance(MyClass1, position); // this gets returned first
    } else if (position == 1){
        fragment = MyFragment.newInstance(MyClass2, position); // this gets returned second
    } else if (position == 2){
        fragment = MyFragment.newInstance(MyClass3, position); // this gets returned third and is the only fragment displayed in the 3 tabs
    }
    return fragment;
}

I believe the issue must be somewhere there, but tell me if I need to share/change other parts of code

EDIT: This is my newInstance() function in my fragment class

public static MarkStudentFragment newInstance(MyClass inputClass, int inputPosition) {
    MyFragment fragment = new MyFragment();
    dataClass = inputClass;
    position = inputPosition;
    return fragment;
}

dataClass then gets used to set up the layout of the fragment, etc

Enrique Tasa
  • 25
  • 1
  • 7
  • You can get rid of the `new MyFragment()`, as you are not using that object. Beyond that, I believe that your problem lies in your `newInstance()` method and how you are using those parameters. – CommonsWare Mar 16 '19 at 11:59
  • can you tell me have you 3 different fragments ? – niceumang Mar 16 '19 at 11:59
  • @ Niceumang yes, 3 different fragments get created and dealt with in system's FragmentPagerAdapter.class. @CommonsWare edited question with newInstance() method. – Enrique Tasa Mar 16 '19 at 12:04
  • First, the `newInstance()` method that you are calling takes two parameters, not one. Second, AFAIK, your `newInstance()` method will not compile, as a `static` method has no `this` object. – CommonsWare Mar 16 '19 at 12:06
  • @CommonsWare added `this.` in an attempt to make code clearer, code does compile, apologies for lack of clarity. As to the two parameters, what do you mean exactly, is this something I should have read in documentation and implemented, or? – Enrique Tasa Mar 16 '19 at 12:10
  • "As to the two parameters, what do you mean exactly" -- in `MyFragment.newInstance(MyClass1, position)`, you are passing two parameters. In `public static MarkStudentFragment newInstance(MyClass inputClass)`, you are accepting one parameter. One != two. So, `public static MarkStudentFragment newInstance(MyClass inputClass)` is not what is being called by `MyFragment.newInstance(MyClass1, position)`. – CommonsWare Mar 16 '19 at 12:16
  • @CommonsWare gotcha, once again, a mistake I made when editing my code for the question, the newInstance() does take two parameters, edited the question to showcase that. Again, apologies for the confusion. – Enrique Tasa Mar 16 '19 at 12:23

3 Answers3

0
public static MarkStudentFragment newInstance(MyClass inputClass, int inputPosition) {
    MyFragment fragment = new MyFragment();
    dataClass = inputClass;
    position = inputPosition
    return fragment;
}

If this actually compiles, it is because you have declared dataClass and position as static. So, each time you call newInstance(), you overwrite the previous dataClass and position values. And each of your fragments will use the one-and-only value of dataClass and position.

To fix this, get rid of the dataClass and position fields, and use the arguments Bundle. Note that you will need to switch from passing an instance of MyClass to passing some identifier that will allow the fragment to get the proper data (e.g., an enum).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
-1

We have a method in fragment called getUsetVisibleHint().. by using this when the fragment is visible we can call the api or fetch details and attach to layout..

satyan_android
  • 364
  • 4
  • 15
-1

you can put code like this

 public Fragment getItem(int i)
{
    switch (i)
    {
        case 0:
             Fragment fragment1=new Fragment();
            return fragment1;
        case 1:
            Fragment fragment2=new Fragment();
            return fragment2;
        case 2:
            Fragment fragment3=new Fragment();
            return fragment3;
        default:
                return null;

    }
}
niceumang
  • 1,347
  • 1
  • 8
  • 21