I am creating a ViewPager with three views. The first view is a ListView, where onItemClick replaces this fragment with a new fragment using WebView. And I have two problems: when adding "transaction.addToBackStack (null)" or "transaction.setCustomAnimations (...)" Fragment with WebView stops to show. Without these lines, this shows, but I canβt make a BACK button to return to the list.
I created the code based on this tutorial and this SO question . This question uses the PagerAdapter, and I use the FragmentPagerAdapter. Also I don't need any more fragments that need replacing. It can be only once (from the list in webView and back).
Here is my adapter 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 a fragment from onCreateView () of this list 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 ); } } } } );
source share