0

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

user2582318
  • 1,607
  • 5
  • 29
  • 47
  • 1
    try read this http://stackoverflow.com/questions/6315908/which-activity-method-is-called-when-orientation-changes-occur – Randyka Yudhistira Jan 25 '17 at 01:31
  • @RandykaYudhistira same result, impossible to have Dynamic Viewpager and screen rotation and keep the original fragments, i ended using -> android:configChanges: =( – user2582318 Jan 25 '17 at 02:07

0 Answers0