20

I use a FragmentPagerAdapter containing several Fragments that need to be notified about changes to the database. I do this by iterating over the fragments, which implement a callback interface, and calling a refreshData() method.

This works just fine until the device changes orientation. After an orientation change, the fragment UI does not visibly refresh even though the method call seems to work.

From what I have read so far this occurs because the FragmentPagerAdapter handles the fragment life-cycle and the fragments that receive the callback are not the ones that are actually displayed.

private class DataPagerAdapter extends FragmentPagerAdapter {

    private DataFragment[] fragments = new DataFragment[] {
                                     new FooDataFragment(), BarDataFragment()};

    public DataPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments[position];
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    public DataFragment[] getFragments() {
        return fragments;
    }
}

protected void refreshData() {
    for (DataFragment dataFragment : mPagerAdapter.getFragments()) {
     dataFragment.refreshData();
}

I temporarily fixed this issue using a broadcast receiver inside each fragment, but this solution seems to be wasteful and might create memory leaks. How to I fix this properly? I use a different layout and logic in landscape mode so I want to use the newly created fragments after an orientation change.

AStopher
  • 4,207
  • 11
  • 50
  • 75
Philipp E.
  • 3,296
  • 3
  • 34
  • 52

4 Answers4

59

Yes like you said FragmentManager handles fragments after orientation change so getItem in adapter is not called. But you can override method instantiateItem() which is called even after orientation change and cast Object to Fragment and save it in your array.

 @Override
 public Object instantiateItem(ViewGroup container, int position) {
     DataFragment fragment = (DataFragment) super.instantiateItem(container, position);
     fragments[position] = fragment;
     return fragment;
 }
Koso
  • 3,200
  • 2
  • 20
  • 22
  • 1
    This looks neat, unfortunately the behavior is still them same. I also tried adding android:configChanges="orientation" to the manifest. – Philipp E. Jul 13 '13 at 11:17
  • 2
    I really don't recommend you to use orientantionChanges in manifest. So you are saying refreshData() is still called on old fragments? When do you call refreshData()? You should check if instantiateItem() is really called and is called before refreshData() method. – Koso Jul 13 '13 at 11:26
  • You are right, is not called. Odd. Any suggestions? refreshData() is called long after the fragment instantiation, for example when the user presses a button but it can also be triggered asynchronously by a background service. – Philipp E. Jul 13 '13 at 11:51
  • and did you remove android:configChanges="orientation"?? – Koso Jul 13 '13 at 11:56
  • Oh, forgot to mention this. Yes, I did. – Philipp E. Jul 13 '13 at 11:59
  • 3
    Sorry for late answer man. I updated answer code. You need to use `instantiateItem(ViewGroup container, int position)`, not `instantiateItem(View container, int position)`. Hope it will work now. – Koso Jul 13 '13 at 19:37
  • 10
    @koso, after wasting 5 hours to solve it your solution was damn right! – r4m Nov 14 '13 at 15:59
  • The method to create the fragment name is pretty simple-- I found this on [Grepcode](http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/support/v4/app/FragmentPagerAdapter.java/?v=source): `return "android:switcher:" + viewId + ":" + index;` `viewId` is the containing view's id, and `index` is the fragment's position. Using this I was able to pull the fragments out of the FragmentManager. – bjdodson Oct 14 '14 at 20:54
  • 1
    After wasting 1-2 days in retaining the instance of nest fragment inside viewpager using FragmentStatePagerAdapter, this solution fixed my problem. – Rajen Raiyarela Mar 26 '15 at 10:36
0

Could not you just set the android:configChanges flag on your Activity in AndroidManifest.xml to prevent activity to be recreated?

<activity
    android:name="com.example.test.activity.MainActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"/>

also there you can find more possibilities: https://android.jlelse.eu/handling-orientation-changes-in-android-7072958c442a

Kamil Z
  • 181
  • 12
0

I recreated the pager adapter in the onResume method:

override fun onResume() {
    super.onResume()
    val viewPager = findViewById<ViewPager>(R.id.my_view_pager)
    viewPager.adapter = MyPagerAdapter(supportFragmentManager)
}
Trieu Doan
  • 149
  • 1
  • 3
-1

I had similar problem my working solution: In activity where is an adapter call adapter.notifyDataSetChanged();

override getItemPostion() method in your adapter

@Override
    public int getItemPosition(Object object) {
        MyFragment frag = (MyFragment)object;
        frag.refresh();
        return POSITION_UNCHANGED;
    }

my FragmentStatePagerAdapater (my adapter) contains only one type of fragment, but I am think, that you could use instanceof (for more types of Fragments) and then use cast or every fragment will implement Interface with method refresh()

Then add method refresh() in your Fragment(s) and use this implementation:

public void refresh() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        ViewGroup viewGroup = (ViewGroup) getView();
        viewGroup.removeAllViewsInLayout();
        View view = onCreateView(inflater, viewGroup, null);
        viewGroup.addView(view);

    }

This worked for me. My opinion: The problem has came, when the device change orientation, adapter is destroyed, but the fragments are still in memory and system try to reuse them after orientation change, but the views which are contains in the fragments are destroyed too and fragments needs to be "recreate" (call onCreateView method ). Maybe I am wrong, so please correct me... .

(Sorry for my english )

Pauli
  • 538
  • 8
  • 22
  • This solution won't well in most applications. There are cases when android will re-instantiate your fragment on your behalf as an optimization (e.g. after orientation change). This won't always produce the desired results - for example, if you add your ViewPager outside of the activity's onCreate, the fragment will get a null container. To fix such issues, you can remove the fragment in your fragment's onCreateView() { if(container == null) { removeMeFromFragmentManager(); return null; ... }. Your new FragmentPageAdapter will now recreate this fragment instance. – dzlatkov Apr 05 '15 at 00:34