2

I have two activities with ViewPager, first one manages Fragments with several ListViews, second one has ListView and WebView inside. both onCreate methods in activities looks the same (the only difference is pager class):

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_subcat_list);

    mPager = new SubcatListPager(this);
    mPager.initialisePaging(false);
}

the problem occurs when I change the screen orientation. Activity with ListView fragments works fine, but the second activity creates a fragment with WebView when this line super.onCreate(savedInstanceState); is executed. and then inside pager code that fragment is created one more time. I tracked it in the debugger and two fragments with webviews are created every orientation change. I have fixed the problem by passing null instead of saved instance: super.onCreate(null);, but I don't think this is the right way.

is there a better way to fix this problem and restore state of the fragment I create inside ViewPager? I can use savedInstanceState and pass it to the fragments I create inside pager, but I'm not sure if passing null to the activity's parent is correct.

UPD: ManosProm's solution solves the fragment recreation issue, but now getActivity inside those fragments returns null. Quick search tells it might be caused by fragment being detached from the activity. This is my code:

public CommentsPager(SherlockFragmentActivity fa, Class<? extends SherlockFragment> fragment)
    {
        mFragmentActivity = fa;
        try {
            FragmentManager fm = mFragmentActivity.getSupportFragmentManager();
            mContentFragment = (SherlockFragment) fm.findFragmentByTag(makeFragmentTag(0));
            if (mContentFragment == null)
                mContentFragment = fragment.newInstance();

            mCommentsFragment = (CommentsListFragment) fm.findFragmentByTag(makeFragmentTag(1));
            if (mCommentsFragment == null)
                mCommentsFragment = CommentsListFragment.class.newInstance();
        }
        catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(fa, "Pager creation failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }

    }

    public void initialisePaging(boolean bInLayout) {

        //mFragmentActivity.setTitle(mFragmentActivity.getResources().getString(SiteContent.curr_cat.nameID));

        try {
            List<Fragment> fragments = new Vector<Fragment>();
            mSectionsPagerAdapter = new CommentsPagerAdapter(mFragmentActivity.getSupportFragmentManager(), fragments);

            if (!(mContentFragment instanceof ContentDownloader)) {
                throw new IllegalStateException(
                        "Fragment must implement content downloader interface.");
            }

            fragments.add(mContentFragment);
            fragments.add(mCommentsFragment);

            mViewPager = (ViewPager)mFragmentActivity.findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

            final ActionBar actionBar = mFragmentActivity.getSupportActionBar();
            actionBar.removeAllTabs();

            mViewPager
                    .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });

            for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                Tab tab = actionBar.newTab();
                tab.setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this);
                actionBar.addTab(tab, i == 0);
            }

            //actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayHomeAsUpEnabled(!bInLayout);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

How can I reattach fragments to the activity? Or the code is fine and I should look for the problem elsewhere?

4

1 に答える 1