3

I have a strange UI problem. In brief, I have a ViewPager inside of a Fragment (lets call it 'A'). Up to this point, displaying Fragments inside the ViewPager is no problem.

Problem comes in when I press the back button. The back button pops off Fragment A just fine and returns to the previous Fragment, however during this transition, contents of the ViewPager is completely blanked out.

This is quite evident since I have transition animations. I have no idea why this is happening. It's (seems) pretty clear that it has to do with nested Fragments because this only happens on this particular screen which is the only one with nested Fragments.

Code Snippets

Initial Fragment displays a list of items. We click on one item....

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    FragmentManager fm = getFragmentManager();
    Fragment f = fm.findFragmentByTag("some_tag_name");
    if(f == null) {
        f = SomFragment.newInstance(...);
    }  

    getFragmentManager()
            .beginTransaction()
            .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
                                 R.anim.slide_in_left, R.anim.slide_out_right)
            .replace(R.id.frame, f)
            .addToBackStack(null)
            .commit();
}

Now, a new Fragment appears with a ViewPager. ViewPager is initialized...

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {    
    mPager = (ViewPager) view.findViewById(R.id.viewpager);
    mPagerAdapter = new SomeAdapter(getChildFragmentManager());
    mPager.setAdapter(mPagerAdapter);    
}

I'm not overriding onBackPressed in the parent Activity or any other funny business. It's very straightforward.

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • Nested fragments do behave in weird ways and are officially only supported from android version 17 and up. So i would advise against such an approach, try to find a solution without implementing nested fragments is all i can say. As for you specific issue, a few lines of code would help in analyzing why this is happening. – Sam May 24 '13 at 14:43
  • @SamarthJain -- The code is very straightforward but I'll include it anyway. – Alex Fu May 24 '13 at 15:21
  • possible duplicate of [Nested fragments disappear during transition animation](http://stackoverflow.com/questions/14900738/nested-fragments-disappear-during-transition-animation) – user May 24 '13 at 15:22
  • @AlexFu How you solved this issue. – Harish Nov 16 '15 at 10:03
  • @Harish I think i ended up going with this - http://stackoverflow.com/a/22673871/993416 – Alex Fu Nov 16 '15 at 16:58
  • @AlexFu in vaiepager adapter what you have passed.above you provided link was not worked for me – Harish Nov 17 '15 at 08:56

2 Answers2

0

You need to override onResume() method, and re-initialize your UI there.

@Override
 public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         initialize();
}

private void initialize(){
      btnOne= (Button) getActivity().findViewById(R.id.btnOne);
      btnTwo = (Button) getActivity().findViewById(R.id.btnTwo);      

      btnOne.setOnClickListener(this);
      btnTwo.setOnClickListener(this);    

      viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
      viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));

}

@Override
public void onResume() {
    super.onResume();
    initialize();
}
Ravi
  • 191
  • 2
  • 6
0

Use onCreateView() (not onViewCreated()):

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.pager, container, false);                                                   

   mPager = (ViewPager)rootView.findViewById(R.id.viewpager);
   mPagerAdapter = new SomeAdapter(getChildFragmentManager());
   mPager.setAdapter(mPagerAdapter); 
   return rootView;   
 }
Juli
  • 21
  • 4