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 );
}
}
}
} );
Check out my new answer to mentioned question: https://stackoverflow.com/a/11974777/685292
You can see there an example how to implement simple back stacking with ViewPager by your own since addToBackStack(null) does not work for ViewPager fragments. Also note that main problem about your issue is probably the missing getItemId(int position) overriden method.
In new design support library, i use this solution
You can use stack to store the tabpositon by pushing the position in stack and getting back by using pop in stack.
In the main activity where there are 3 fragment in viewpager i create stack and push and pop data.
private Stack<Integer> stackkk;
private ViewPager mPager;
private int tabPosition = 0;
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tabPosition = tab.getPosition();
mPager.setCurrentItem(tab.getPosition());
if (stackkk.empty())
stackkk.push(0);
if (stackkk.contains(tabPosition)) {
stackkk.remove(stackkk.indexOf(tabPosition));
stackkk.push(tabPosition);
} else {
stackkk.push(tabPosition);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tabPositionUnselected = tab.getPosition();
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
and in the onBackPressed in activity,
@Override
public void onBackPressed() {
if (stackkk.size() > 1) {
stackkk.pop();
mPager.setCurrentItem(stackkk.lastElement());
} else {
//do wat you want
}
}
Hope this may help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With