Good day!
I tried to find the answer to my question on this site, but I could not. The essence of the problem lies in what: 1) There is a fragment that is declared in the layout.
<fragment android:id="@+id/frame_page" android:layout_width="match_parent" android:layout_height="match_parent" class="com.vonegosh.creditcalc.PageFragment" tools:layout="@layout/pageview"> </fragment> 2) There is a PageFragment class that contains a ViewPager and a FragmentStatePagerAdapter. It looks like this:
public class PageFragment extends Fragment { ViewPager mViewPager; View v; SharPref sharPref; private static final String VIEW_PAGER_POSITION = "ViewPagerPosition"; TitleAdapter titleAdapter; public void setAdapter(Calc calc) { if (titleAdapter == null) { titleAdapter = new TitleAdapter(getFragmentManager(), calc, getActivity()); mViewPager.setAdapter(titleAdapter); } else { titleAdapter.updateAdapter(calc); } mViewPager.setCurrentItem(sharPref.getSharedPreferences().getInt(VIEW_PAGER_POSITION, 0)); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.pageview, container, false); mViewPager = (ViewPager) v.findViewById(R.id.pager); PagerTabStrip strip = (PagerTabStrip) v.findViewById(R.id.pagerTabStrip); strip.setTabIndicatorColor(0x228B22); ((ViewPager.LayoutParams) (v.findViewById(R.id.pagerTabStrip)).getLayoutParams()).isDecor = true; mViewPager.setOffscreenPageLimit(4); sharPref = new SharPref(getActivity()); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { sharPref.getEditor().putInt(VIEW_PAGER_POSITION, position).commit(); } @Override public void onPageScrollStateChanged(int state) { } }); return v; } class TitleAdapter extends FragmentStatePagerAdapter { private Bundle b; String[] titles; Calc calc; FragmentView1 fragmentView1; FragmentView2 fragmentView2; android.support.v4.app.FragmentManager fm; TitleAdapter(android.support.v4.app.FragmentManager fm, Calc calc, Context context) { super(fm); this.titles = context.getResources().getStringArray(R.array.данные_для_pageview); this.fm = fm; fragmentView1 = new FragmentView1(); fragmentView2 = new FragmentView2(); b = new Bundle(); b.putParcelable("calc", calc); } void updateAdapter(Calc calc) { this.calc = calc; // ((FragmentView1) fm.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + 0)).updateAdapter(calc); fragmentView1.updateAdapter(calc); // fragmentView2.updateAdapter(calc); } @Override public int getCount() { return 4; } @Override public CharSequence getPageTitle(int position) { return titles[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: fragmentView1.setArguments(b); return fragmentView1; case 1: fragmentView2.setArguments(b); return fragmentView2; case 2: FragmentView3 fragmentView3 = new FragmentView3(); fragmentView3.setArguments(b); return fragmentView3; case 3: FragmentView4 fragmentView4 = new FragmentView4(); fragmentView4.setArguments(b); return fragmentView4; default: return null; } } } } 3) Through the procedure public void setAdapter(Calc calc) activation sends information to the fragment, after which it either creates an adapter for the viewPager or updates the adapter, if it already exists.
public void setAdapter(Calc calc) { if (titleAdapter == null) { titleAdapter = new TitleAdapter(getFragmentManager(), calc, getActivity()); mViewPager.setAdapter(titleAdapter); } else { titleAdapter.updateAdapter(calc); } mViewPager.setCurrentItem(sharPref.getSharedPreferences().getInt(VIEW_PAGER_POSITION, 0)); } - When the TitleAdapter is created, the information is packaged in a Bundle and passed through setArgument to the child fragments. When the TitleAdapter is updated, we pass an instance of the Calc class to the child fragment via the updateAdapter procedure, after which the child fragment FragmentView1 updates the adapter ListView.
In normal mode, everything works perfectly. Now turn the screen. No matter how many times. TitleAdapter re-created. Try to transfer the Calc data again. Since the adapter TitleAdapter already exists, it runs the program branch to update the child fragments and the program crashes NullPointExceprion with an error. The result is that the fragment seems to be recreated, but getActivity, like other links to such elements as listView, is lost and refers to Null. The whole idea with updates is to call notifyDataSetChanged on the ListView adapter in child fragments when this adapter already exists. But after turning the screen, this adapter returns Null, although in the interface everything is visually recreated.
UPD: Understood what the problem is. The fact is that somehow the FragmentStatePagerAdapter maintains the state of the fragments, despite the fact that a new adapter is being invoked. Or I do not understand. But this code helped me @Override public Parcelable saveState() { return null; } @Override public Parcelable saveState() { return null; }