2

UPDATE 2: Getting rid of all v4 support references fixed it. UPDATE: I started from scratch to see what triggers this behavior. It occurs once I add a check for location permissions. I can't go backwards -- even when I strip out all the permissions code it stays with the incorrectly-bahaving FragmentStatePagerAdapger.

I have a FragementStatePagerAdapter that was working just fine for a ViewPager of dynamically created fragments until I changed my compileSdkVersion and target SdkVersion from 22 to 23, using appcompat-v7:23.2.1. Now if I attempt to load, say, A, B, C it loads B, B, C. But then if I swipe back I get C, B, A. So it is only the initial attempt to load dynamically-created fragment A that is unsuccessful.

Here is how I set my adapter and viewpager:

myAdapter = new MyAdapter(getSupportFragmentManager(), numItems);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(myAdapter);
viewPager.setCurrentItem(position);

MyAdapter:

private class MyAdapter extends FragmentStatePagerAdapter {
    private final int size;

    public MyAdapter(FragmentManager fm, int _size)  {
        super(fm);
        size = _size;
    }

    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Fragment getItem(int position) {
        String _id = myArray[position];
        return MyFragment.newInstance(_id);
    }
}

And instantiating the Fragment:

public static MyFragment newInstance(String _id)  {
        final MyFragment f = new MyFragment();
        final Bundle args = new Bundle();
        args.putString("_id", _id);
        f.setArguments(args);
        return f;
    }

...

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _id = getArguments().getString("_id");            
    }

Has anyone else experienced this after upgrading? I am at a total loss after spinning my wheels on this for hours.

alice_silver_man
  • 412
  • 5
  • 19
  • 1
    Just an idea after a quick check of a simular code in my project. Are you sure you need SupportFragmentManager? I use the straight FragmentManager and it works just fine for recent appcompat and SDK 23... – DmitryO. Mar 29 '16 at 22:05
  • Holy cow, that fixed it. YES!! Put this as the answer and I will accept it. – alice_silver_man Mar 29 '16 at 22:42

2 Answers2

2

The idea that I posted as a comment resolved the problem, here is the same answer with a few more details...

Short version: in adapters derived from FragmentStatePagerAdapter, try to use FragmentManager instead of SupportFragmentManager. Unless you're 100% sure you need the SupportFragmentManager.

Explanation:

code in the question looks pretty good. The only place where adapter can 'confuse' fragments is the method instantiateItem(ViewGroup container, int position). This method uses a FragmentManager passed as an argument of constructor. So we can blame that suspicious SupportFragmentManager.

Community
  • 1
  • 1
DmitryO.
  • 973
  • 5
  • 8
  • To follow up, I did not want to use the v13 support library, so I had to copy these files into my project (your sdk folder --> sources --> android-23 --> android --> support --> v13 --> app): FragmentCompat, FragmentCompat23, FragmentCompatICS, FragmentCompatCSMR1, FragmentStatePagerAdapter. – alice_silver_man Mar 30 '16 at 20:15
  • Right, I target on API 16+, so I ended up copying just FragmentStatePagerAdapter with a minor custom tweak. The rest works fine with native Fragment (crossing my fingers). – DmitryO. Mar 30 '16 at 20:55
  • @silver_man Just to be clear, you should use FragmentManager if you are using `android.app.Fragment`, and use SupportFragmentManager if you are using `android.support.v4.app.Fragment`, simple as that. Both can be used in conjunction with the Support Library, i.e. you can put either type of Fragment in an AppCompatActivity. Also note that if you want to use `android.app.Fragment` native Fragments with a FragmentPagerAdapter or FragmentStatePagerAdapter, you need to use the v13 support library versions, i.e. `android.support.v13.app.FragmentStatePagerAdapter`. – Daniel Nugent Mar 31 '16 at 04:24
  • Thanks @DanielNugent. I was using the support Fragment for no reason other than I had not updated my code since going to API 16+. I got around using the v13 support library by copying that and the other required support classes directly into my project. – alice_silver_man Apr 02 '16 at 01:57
0

To add to the answer from DmitryO, you should use FragmentManager if you are using android.app.Fragment, and use SupportFragmentManager if you are using android.support.v4.app.Fragment, simple as that.

Both can be used in conjunction with the Support Library, i.e. you can put either type of Fragment in an AppCompatActivity. Also note that if you want to use android.app.Fragment native Fragments with a FragmentPagerAdapter or FragmentStatePagerAdapter, you need to use the v13 support library versions of the adapter, i.e. android.support.v13.app.FragmentStatePagerAdapter.

Note, this was originally posted as a comment, but I didn't want this info to get lost.

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137