I'm creating ViewPager with three viewsin it. On the first view is a ListView, where onItemClick replaces that fragment new fragment with WebView in it. And I have two problems: when adding 'transaction.addToBackStack(null)', or 'transaction.setCustomAnimations(...)' Fragment with WebView stopps to show. Without these lines it shows, but I can't do BACK button to return to list.
I was creating code based on that tutorial and this SO question. In that question there is used PagerAdapter, and I'm using FragmentPagerAdapter. Also I don't need more fragments to be replaced. It can be only once (from list to webView and back).
Here is my adapter's code:
public Fragment getItem( int position )
{
Fragment fragment = null;
switch ( position )
{
case 0: // news
if ( mFragmentAtPos0 == null )
{
mFragmentAtPos0 = new NewsListFragment( new FirstPageFragmentListener()
{
@Override
public void onSwitchToNewsFragment( String url, ViewGroup container )
{
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations( R.anim.rotate_in, R.anim.rotate_out, R.anim.rotate_in, R.anim.rotate_out );
WebViewFragment wv = new WebViewFragment( url, context );
transaction.replace( container.getId(), wv );
transaction.commit();
mFragmentAtPos0 = wv;
notifyDataSetChanged();
}
} );
}
fragment = mFragmentAtPos0;
break;
case 1:
fragment = new NewsInfoFragment( context, true );
break;
case 2:
fragment = new NewsInfoFragment( context, false );
break;
}
return fragment;
}
and snippet from onCreateView() of that List's fragment:
listView.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id )
{
if ( position < listNewsAdapter.getCount() - 1 )
{
if ( listener != null )
{
listener.onSwitchToNewsFragment( listNewsAdapter.getItem( position ).getNewsUrl(), container );
}
}
}
} );