Android - Backstack when replacing a fragment in ViewPager

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 ); } } } } ); 
+6
source share
2 answers

Check out my new answer to the mentioned question: fooobar.com/questions/28488 / ...

You can see an example of using simple reverse stacking with ViewPager as you see fit, since addToBackStack (null) does not work for ViewPager fragments. Also note that the main problem with your problem is probably the missing getItemId (int position) overriden method.

+2
source

I use this solution in the new design support library
You can use the stack to hold tabpositon by pushing a position on the stack and returning using pop on the stack.

In the main activity, where there are 3 fragments in the viewpager, I create a 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 onBackPressed in action,

 @Override public void onBackPressed() { if (stackkk.size() > 1) { stackkk.pop(); mPager.setCurrentItem(stackkk.lastElement()); } else { //do wat you want } } 

Hope this helps

0
source

Source: https://habr.com/ru/post/916431/


All Articles