OncreateView does not call when the action is destroyed and re-started

I am having a strange problem with Fragmentand ViewPager.

I have 1 ViewPager contains 3 fragments.

  • When my application launches for the first time. onCreateView is called on all my fragments and ViewPager works fine.

ViewPager initialization code

private void initPager() { 

    mFragmentList.add(new MainSongListFragment());


    mFragmentList.add(new MainAlbumArtFragment());
    mFragmentList.add(new MainLyricsFragment());

    if (mFragmentList == null)
        mFragmentList = new ArrayList<Fragment>();

    MainPagerAdapter pagerAdapter = new MainPagerAdapter(
            getSupportFragmentManager(), mFragmentList);
    mMainPager.setAdapter(pagerAdapter);

    myListViewInsideFragment= (ListView) findViewById(R.id.my_list);
}
  • When I press the button again and run the application again. I get an error in the code:

    myListViewInsideFragment = (ListView) findViewById (R.id.my_list);

It returns null because the onCreateView method on the fragment is not called, so all views of this fragment are not available.

Code in my snippet:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.lo_my_list, container,
            false);
    mListView = (ListView) view
            .findViewById(R.id.my_list);
    return view;
}

My question is: Why is the onCreateView method not called when the Activity stops working and starts again?

Thank.

+3

All Articles