I am writing an Android application for Api 10-23 using Appcompat. In MainActivity there is a ViewPager on 3 tab fragments. Code like this:
public class MainActivity extends AppCompatActivity { private TabLayout tabLayout; private TabsFragmentAdapter tabsFragmentAdapter; private void initToolbar() { Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setTitle("Looker"); } private void initTabs() { ViewPager viewPager=(ViewPager) findViewById(R.id.viewPager); tabsFragmentAdapter= new TabsFragmentAdapter(getSupportFragmentManager() ); viewPager.setAdapter(tabsFragmentAdapter); tabLayout= (TabLayout)findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); DbHelper dbHelper= new DbHelper(this); dbHelper.initDb(); DbHelper.initCursors(); initToolbar(); initTabs(); } } public class TabsFragmentAdapter extends FragmentPagerAdapter { private HashMap<Integer,MainListFragment> tabs; public TabsFragmentAdapter(FragmentManager fm) { super(fm); initTabsMap(); } @Override public CharSequence getPageTitle(int position) { return tabs.get(position).getTitle(); } @Override public Fragment getItem(int position) { return tabs.get(position); } @Override public int getCount() { return tabs.size(); } private void initTabsMap() { tabs = new HashMap<>(); MainListFragment fragmentFilms= new Fragment_Films(); MainListFragment fragmentSerial= new Fragment_Serial(); MainListFragment fragmentMult= new Fragment_Serial(); fragmentFilms.initFragment("Фильмы",O.interaction.CONTENT_FILMS, R.layout.main_list_element_film); fragmentSerial.initFragment("Сериалы",O.interaction.CONTENT_SERIAL, R.layout.main_list_element_serial); fragmentMult.initFragment("Мульт-сериалы",O.interaction.CONTENT_MULT, R.layout.main_list_element_serial); tabs.put(O.interaction.CONTENT_FILMS, fragmentFilms); tabs.put(O.interaction.CONTENT_SERIAL, fragmentSerial); tabs.put(O.interaction.CONTENT_MULT, fragmentMult); } } public abstract class MainListFragment extends Fragment implements AdapterInterface { private String title; public int contentType; protected int listElementLayout; public void initFragment(String _title,int _contentType,int _listElementLayout) { title=_title; contentType=_contentType; listElementLayout=_listElementLayout; } public void initAdapter() { ... } @Nullable @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { super.onCreateView(inflater,container,savedInstanceState); View view= inflater.inflate(R.layout.main_list_fragment,container, false); return view; } } A lot of code, like content fragments, behind the scenes, of course. The main thing is that their initialization occurs from the base, where contentType points to a table, to the desired cursor.
Actually, the problem is:
When, for example, a screen rotation takes place, or maybe I went ahead for a couple of activities, and then did a taskKill , the MainActivity reloads and fragments are re-created. After that, the application crashes from cursor errors that look in the wrong tables. I was looking for a bug: changed the constants for contentType to 4,5,6 and found out that during a normal start the following sequence is executed:
onCreate: main initTabsMap: TabsFragmentAdapter initFragment: 4 initFragment: 5 initFragment: 6 getItem: 4 getItem: 5 onCreateView: 4 initAdapter: 4 onCreateView: 5 initAdapter: 5 When rebooting:
onCreate: main initTabsMap: TabsFragmentAdapter initFragment: 4 initFragment: 5 initFragment: 6 onCreateView: 0 onCreateView: 0 initAdapter: 0 initAdapter: 0 In the sense, fragments are created, initialized, but getItem not called and someone (maybe Fragment Manager, I do not know) gives me empty fragments. Tell me why this happens and what to do about it?
initFragment, and then do not use them. It is also not clear why to callsuper.onCreateView. As far as I understand you, the problem is in your initAdapter, and you did not provide its code. - YurySPb ♦initAdapter(). Simply there the code with which lists in fragments throughDbHelperand cursors are initialized. And there arrays of fields and id forCustomCursorAdapterin one of the options presented in the heirs. I was afraid to mention too much, because then I would have to add a bunch of code for the full picture, it would be difficult to stop, but it is enough. It is superfluous. In this case, it suffices thatinitAdapter()used in thisinitAdapter(). Without them there will be mistakes. - CC-Ultra