Fragment.onCreateView () not called in viewpager after configuration change

After changing the orientation, my activity with the viewpager, which is a list / part view, is recreated and reset if I select an item in the list.

I don't want to bother you with all the details (there is a nullpointerexception exception, because the text view referenced in onCreateView () is null). My question is: how does the ViewPager display 2 pages with a fragment in each of them, without even calling them onCreateView ()?

How does it work after starting the application before turning on the device?

I only have 2 pages / snippets. But I saw with the debugger that 4 fragments are created when this activity is recreated. Do you know why and where is this documented?

Any solution? Thanks in advance for your help. I get lost here, because I myself do not control the fragments, the viewer does.

===========

Update: please find additional requested information.

public class  DetailFragment extends Fragment {
    TextView mDetailTV;
    // Retrieve the data to display from the parent activity
    private String getData() {
        ViewPagerActivity activity = (ViewPagerActivity)getActivity();
        return( activity.detailArray[activity.mPosition] );
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_detail_fragment, container, false);
        mDetailTV = (TextView)view.findViewById(R.id.detailTV);
        displayDetail(getData());   
        return view;
    }
    // Display data
    void displayDetail(String data) {       
        mDetailTV.setText(data);
    }

and fragments of the list and parts are created and transferred to the Pager Adapter as follows:

mListFragment = new ListFragmentt();
mDetailFragment = new DetailFragment();
mPagerAdapter = new MFragmentPagerAdapter(getSupportFragmentManager(), mListFragment, mDetailFragment);

In addition, the data from the list / details is taken from the classic Skakespeare example, so I doubt that my Samsung S4 is out of memory.

+4
source share
2 answers

I fixed my problem by removing the onSaveInstanceState call of the FragmentActivity superclass, for example:

protected void FragmentActivity (Bundle outState) {}

, FragmentActivity. , . , . - , .

+1

ViewPager 2 . onCreateView .

0

Source: https://habr.com/ru/post/1530781/


All Articles