im trying to implement ViewPager + Adapter, without configChanges at the manifest, the result:
When i rotate the screen, the fragments views become null and it remove other views showing only one tab
my Adapter
private class TabsPagerAdapter extends FragmentStatePagerAdapter {
private Bundle bundle;
private String titulo;
private List<LeiAberta> fragments;
private TabsPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<>();
}
@Override
public LeiAberta getItem(int position) {
return fragments.get(position);
}
private void addPage(LeiAberta page) {
page.setRetainInstance(true);
fragments.add(page);
notifyDataSetChanged();
}
public void removePage(int position) {
if(fragments.size() <= 1) {
finish();
} else {
fragments.remove(position);
notifyDataSetChanged();
}
}
@Override
public int getItemPosition(Object object) {
if (fragments.contains(object)) return fragments.indexOf(object);
else
return POSITION_NONE;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
this.bundle = fragments.get(position).getArguments();
if (this.bundle != null) {
this.titulo = bundle.getString("titulo");
}
return titulo;
}
}
when activity is created:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leitura);
tabs = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setOffscreenPageLimit(20);
realm = Realm.getDefaultInstance();
bundle = getIntent().getExtras();
if (bundle != null && bundle.getInt("id") > 0) {
texto = RepositoryTexto.getActiveTextoById(realm, bundle.getInt("id"));
bundle = new Bundle();
bundle.putInt("id", texto.getId());
bundle.putString("titulo", texto.getTitulo());
leiAberta = new LeiAberta();
leiAberta.setArguments(bundle);
adapter.addPage(leiAberta);
}
at the first run, the views at the fragment are working well, when i rotate, it become null
is there any way to keep the same viewpager, adapter and fragments without recreating it? i already have onSaveInstance and restore but doesnt matter, since the getActivity become null at the fragment too