Fragment Field is NULL after rotate device in Android












0















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);
}
}
}









share|improve this question

























  • 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

    – 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

    – Erik Hellberg
    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
















0















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);
}
}
}









share|improve this question

























  • 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

    – 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

    – Erik Hellberg
    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














0












0








0








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);
}
}
}









share|improve this question
















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);
}
}
}






android android-fragments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 14:16







Erik Hellberg

















asked Dec 30 '18 at 14:50









Erik HellbergErik Hellberg

6711921




6711921













  • 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

    – 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

    – Erik Hellberg
    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



















  • 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

    – 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

    – Erik Hellberg
    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

















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

– Ishtdeep Hora
Dec 30 '18 at 15:07





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

– 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

– Erik Hellberg
Dec 30 '18 at 15:19





I know they are recreated and thats why I set the field in the Fragment onViewCreated(), but still after rotate that field is null

– Erik Hellberg
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







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





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 Answers
4






active

oldest

votes


















3














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.






share|improve this answer



















  • 1





    Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

    – Dmitro 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?

    – Erik Hellberg
    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

    – Erik Hellberg
    Dec 30 '18 at 17:14





















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).






share|improve this answer
























  • 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

    – Erik Hellberg
    Dec 30 '18 at 16:17



















0














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.






share|improve this answer


























  • 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








  • 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



















-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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978606%2ffragment-field-is-null-after-rotate-device-in-android%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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.






    share|improve this answer



















    • 1





      Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

      – Dmitro 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?

      – Erik Hellberg
      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

      – Erik Hellberg
      Dec 30 '18 at 17:14


















    3














    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.






    share|improve this answer



















    • 1





      Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

      – Dmitro 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?

      – Erik Hellberg
      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

      – Erik Hellberg
      Dec 30 '18 at 17:14
















    3












    3








    3







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 30 '18 at 16:21









    Greg MoensGreg Moens

    51718




    51718








    • 1





      Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

      – Dmitro 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?

      – Erik Hellberg
      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

      – Erik Hellberg
      Dec 30 '18 at 17:14
















    • 1





      Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

      – Dmitro 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?

      – Erik Hellberg
      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

      – Erik Hellberg
      Dec 30 '18 at 17:14










    1




    1





    Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

    – Dmitro Ivanov
    Dec 30 '18 at 16:24





    Yeah, nice explanation. Almost problem is people has basic misunderstanding with Lifecycle and how component react with him.

    – Dmitro Ivanov
    Dec 30 '18 at 16:24




    1




    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





    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?

    – Erik Hellberg
    Dec 30 '18 at 17:00







    Thanks sounds like you are right, I will try it, did you look at my sample app code I included?

    – Erik Hellberg
    Dec 30 '18 at 17:00






    1




    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





    @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

    – Erik Hellberg
    Dec 30 '18 at 17:14







    ok nice, that app was designed to run as portrait only, i now try to change that

    – Erik Hellberg
    Dec 30 '18 at 17:14















    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).






    share|improve this answer
























    • 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

      – Erik Hellberg
      Dec 30 '18 at 16:17
















    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).






    share|improve this answer
























    • 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

      – Erik Hellberg
      Dec 30 '18 at 16:17














    0












    0








    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).






    share|improve this answer













    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).







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 30 '18 at 16:06









    Dmitro IvanovDmitro Ivanov

    70848




    70848













    • 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

      – Erik Hellberg
      Dec 30 '18 at 16:17



















    • 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

      – Erik Hellberg
      Dec 30 '18 at 16:17

















    adding more complexity won't solve the actual problem.

    – Martin Zeitler
    Dec 30 '18 at 16:17





    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

    – Erik Hellberg
    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

    – Erik Hellberg
    Dec 30 '18 at 16:17











    0














    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.






    share|improve this answer


























    • 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








    • 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
















    0














    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.






    share|improve this answer


























    • 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








    • 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














    0












    0








    0







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 30 '18 at 16:59

























    answered Dec 30 '18 at 16:07









    EpicPandaForceEpicPandaForce

    48.5k14128253




    48.5k14128253













    • 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








    • 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



















    • 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








    • 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

















    this.mFragments might belong into the FragmentAdapter.

    – Martin Zeitler
    Dec 30 '18 at 16:15





    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





    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







    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







    @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






    2




    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





    @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











    -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





    share|improve this answer




























      -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





      share|improve this answer


























        -3












        -3








        -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





        share|improve this answer













        //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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 15:47









        AmitAmit

        123




        123






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978606%2ffragment-field-is-null-after-rotate-device-in-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas