Note that there are some explanatory texts on larger screens.

plurals
  1. POorientation change with webview in view pager
    text
    copied!<p>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):</p> <pre><code>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); } </code></pre> <p>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 <code>super.onCreate(savedInstanceState);</code> 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: <code>super.onCreate(null);</code>, but I don't think this is the right way.<br><br> 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.</p> <p>UPD: <a href="https://stackoverflow.com/a/17905388/1417782">ManosProm's solution</a> 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:</p> <pre><code>public CommentsPager(SherlockFragmentActivity fa, Class&lt;? extends SherlockFragment&gt; 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&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;(); 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 &lt; 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(); } } </code></pre> <p>How can I reattach fragments to the activity? Or the code is fine and I should look for the problem elsewhere?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload