2

When I start the app everything works ok but when I rotate to landscape it crashes because in the Fragment there is a field that is NULL.

I dont use setRetainInstance(true) or adding Fragments to FragmentManagerI create new Fragments on app start and when app rotate.

In the Activity OnCreate() I create the Fragment and adding them to the viewPager like this.

   protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         ParentBasicInfoFragment parentBasicInfoFragment = new ParentBasicInfoFragment();
         ParentUTCFragment parentUTCFragment = new ParentUTCFragment();
         ParentEventsFragment parentEventsFragment = new ParentEventsFragment();
         this.mFragments = new ArrayList<>();
         this.mFragments.add(parentBasicInfoFragment);
         this.mFragments.add(parentUTCFragment);
         this.mFragments.add(parentEventsFragment);
         this.viewpage.setOffscreenPageLimit(3);
         setCurrentTab(0);
         this.viewpage.setAdapter(new MainActivityPagerAdapter(getSupportFragmentManager(), this.mFragments));
    }

Then I have a test button on the app that when I press it will do like

  public void test(View view) {
      ((BaseFragment) MainActivity.this.mFragments.get(MainActivity.this.viewpage.
                getCurrentItem())).activityNotifiDataChange("hello");
  }

This will work and the current Fragments in the ViewPager have the method, activityNotifiDataChange() that are being called and all is ok.

When I rotate the app and do the same thing pressing the button the activityNotifiDataChange() is being called alright but there a null pointer exception because the ArrayList<Fragment> mFragment is now NULL.

Here´s a small sample Android Studio project showing this behavior: https://drive.google.com/file/d/1Swqu59HZNYFT5hMTqv3eNiT9NmakhNEb/view?usp=sharing

Start app and press button named "PRESS TEST", then rotate device and press the button again and watch the app crash

UPDATE SOLUTION thanks @GregMoens and @EpicPandaForce

public class MainActivityPagerAdapter extends PersistenPagerAdapter<BaseFragment> {

    private static int NUM_ITEMS = 3;

    public MainActivityPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return ParentBasicInfoFragment.newInstance(0, "Page # 1");
            case 1:
                return ParentUTCFragment.newInstance(1, "Page # 2");
            case 2:
                return ParentEventsFragment.newInstance(2, "Page # 3");
            default:
                return null;
        }
    }
}

public abstract class PersistenPagerAdapter<T extends BaseFragment> extends FragmentPagerAdapter {
    private SparseArray<T> registeredFragments = new SparseArray<T>();

    public PersistenPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public T instantiateItem(ViewGroup container, int position) {
        T fragment = (T)super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public T getRegisteredFragment(ViewGroup container, int position) {
        T existingInstance = registeredFragments.get(position);
        if (existingInstance != null) {
            return existingInstance;
        } else {
            return instantiateItem(container, position);
        }
    }
}
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76
  • It is because when the orientation changes the fragment is re-created from the start. If you want to regain the variable's value you need to set the reference either with the setInstantState(true) as you mentioned or context. See this link for reference [link](https://stackoverflow.com/questions/15744445/fragments-reference-to-mactivity-becomes-null-after-orientation-change-ineffec) – Ishtdeep Hora Dec 30 '18 at 15:07
  • I know they are recreated and thats why I set the field in the `Fragment` `onViewCreated()`, but still after rotate that field is null – Tord Larsen Dec 30 '18 at 15:19
  • only manipulate the view when `if(savedInstanceState == null)`... else the code with run again. especially that `setCurrentTab(0);` defeats the idea of rotating the screen and still expecting to see the same tab (`savedInstanceState` could also be used to remember the actual current tab). – Martin Zeitler Dec 30 '18 at 15:59
  • `this.mFragments = new ArrayList<>();` because this is wrong. You should never hold a reference to the fragment list. Just return new instance of Fragment from `getItem(int position)` of `FragmentAdapter` and you are good to go – EpicPandaForce Dec 30 '18 at 16:05

4 Answers4

5

The main problem I see with your app is your misunderstanding with how FragmentPagerAdapter works. I see this a lot and it's due to lack of good javadocs on the class. The adapter should be implemented so that getItem(position) returns a new fragment instance when called. And then getItem(position) will only be called by the pager when it needs a new instance for that position. You should not pre-create the fragments and pass then into the adapter. You should also not be holding strong references to the fragments from either your activity or from parent fragments (like ParentBasicInfoFragment). Because remember, the fragment manager is managing fragments and you are also managing fragments by newing them and keeping references to them. This is causing a conflict and after rotation, you are trying to invoke activityNotifiDataChange() on a fragment that is not actually initialized (onCreate() was not called). Using the debugger and tracking object IDs will confirm this.

If you change your code so that the FragmentPagerAdapter creates the fragments when they are needed and don't store references to fragments or lists of fragments, you will see much better results.

Greg Moens
  • 1,705
  • 8
  • 15
  • 1
    Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him. – Dmytro Ivanov Dec 30 '18 at 16:24
  • 1
    It doesn't help that the javadoc on FragmentPagerAdapter.getItem(position) is very misleading, if not completely wrong. The example implementation they show in the javadocs for FragmentPagerAdapter is correct. – Greg Moens Dec 30 '18 at 16:33
  • Thanks sounds like you are right, I will try it, did you look at my sample app code I included? – Tord Larsen Dec 30 '18 at 17:00
  • 1
    @ErikHellberg I did. I think the only challenge you will run into is the code that depended on having access to mFragments. That code will have to be changed to go through the fragment manager to find the fragment you are looking for. – Greg Moens Dec 30 '18 at 17:13
  • ok nice, that app was designed to run as portrait only, i now try to change that – Tord Larsen Dec 30 '18 at 17:14
  • i had a different issue and this opened my mind. thanks – Richard Muvirimi Jan 29 '21 at 17:23
1
this.mFragments = new ArrayList<>();

Because this is wrong. You should never hold a reference to the fragment list if you are using ViewPager. Just return new instance of Fragment from getItem(int position) of FragmentPagerAdapter and you are good to go.

But to fix your code, you must delete mFragments entirely.

See https://stackoverflow.com/a/58605339/2413303 for more details.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • `this.mFragments` might belong into the `FragmentAdapter`. – Martin Zeitler Dec 30 '18 at 16:15
  • It should not, `super.onCreate` manages fragment recreation after process death. You should not manually create them elsewhere. – EpicPandaForce Dec 30 '18 at 16:33
  • `FragmentPagerAdapter` has `Parcelable saveState()` and `restoreState(Parcelable state, ClassLoader loader)` (so it can handle it's own state), as well as `abstract Fragment getItem(int position)` - and somehow they need to be kept, in order not always to return a `new Fragment` (which would be the only option available, when not keeping them). – Martin Zeitler Dec 30 '18 at 16:40
  • @MartinZeitler you should check the source code. `getItem()` is only called when `instantiateItem` doesn't find the Fragment based on the tag that it creates, which is built as `"android:switcher:" + viewId + ":" + itemId`. If you create Fragments outside of the FragmentPagerAdapter manually, then you WILL end up with *unused* fragments you are holding in your hand, BUT the fragment pager adapter will use the fragments recreated by the SYSTEM and NOT YOURS. – EpicPandaForce Dec 30 '18 at 16:56
  • I've actually checked the source code... merely to see if there is something alike `mFragments` or `mItems`. always returning `new Fragment` seems a little disconnected from reality - because it does not match the expected behavior; I'd agree with letting the `FragmentPagerAdapter` handle their instances (at least to the count of the off-screen limit * two directions to swipe). – Martin Zeitler Dec 30 '18 at 17:03
  • @EpicPandaForce How come when I start the app it works regarding the "If you create Fragments outside of the FragmentPagerAdapter manually, then you WILL end up with unused fragments". I would like to see a more deeper explanation about this if you have time in your answer (I have included in my Q the Google drive app source code) :) – Tord Larsen Dec 30 '18 at 17:11
  • 2
    @ErikHellberg because the first time the app launches, the fragments you create manually are the same fragments that the adapter returns and the same fragments the fragment manager is managing. After rotation, the fragment manager re-creates the fragments AND you re-create the fragments and now there's a disconnect. – Greg Moens Dec 30 '18 at 17:23
  • Crap :) Thanks @GregMoens finally someone who put words on the problem. I would have thought that when app start(after rotate) it should behave the same in my case, but I did not know about FragmentPagerAdapter behavior – Tord Larsen Dec 30 '18 at 17:28
  • @MartinZeitler there isn't because it always finds the fragment by tag. Which is the reliable solution after low memory process termination – EpicPandaForce Dec 30 '18 at 17:38
0

Use setRetainInstance(true) is not a good approach. If you need to same some simple information such as: position of recyclerView, selected item of recyclerView, maybe some model(Parcelable) you could do it with method onSaveInstanceState / onRestoreInstanceState, there is one limitation is 1MB. Here is an example with animations how it works.

For more durable persistance use SharedPreferences, Room(Google ORM) or you could try to use ViewModel with LiveData (best approach some data which should live while user session).

Dmytro Ivanov
  • 1,260
  • 1
  • 8
  • 10
  • adding more complexity won't solve the actual problem. – Martin Zeitler Dec 30 '18 at 16:17
  • I dont want to persist any data except maybe for the current `ViewPager` tab. I want everything to be recreated since they are dum views backed up by the `LiveData`. My `LiveData` will after rotate just keep feeding my Fragment views. In my simple sample the LiveData setup is not included for simplicity – Tord Larsen Dec 30 '18 at 16:17
-3
//change in AndroidManifest.xml file,
<activity
        android:name=".activity.YourActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="sensor"
         />

//YourActivity in which you defines fragment.
 //may be it helps
Amit
  • 72
  • 4