2015-11-19 132 views
0

我有一個TabsFragment,用於顯示FragmentStatePagerAdapter。要顯示選項卡,我首先創建一個TabsFragment,它創建存儲在選項卡中的多個片段。問題是,當我銷燬一個TabsFragment並創建一個新的時,由於某種原因,由以前的TabsFragment創建的Tab碎片被保存在內存中。所以看起來像我的TabsFragment類:如何通過刪除父片段來刪除與FragmentStatePagerAdapter相關的片段

public class TabsFragment extends AbstractFragment { 
    public static final String TAG = "Tabs_Fragment"; 
    private ViewPagerParallax viewPager; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_tabs, container, false); 
     viewPager = (ViewPagerParallax)view.findViewById(R.id.viewpager); 
     viewPager.set_max_pages(CustomFragmentPagerAdapter.PAGE_COUNT); 
     viewPager.setBackgroundAsset(R.drawable.background); 
     viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager())); 
     PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs); 
     tabsStrip.setViewPager(viewPager); 
     return view; 
    } 
    ... 
} 

喜歡我與其他一些片段取代我TabsFragment:

public void transactOver(AbstractFragment what, int direction) { 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.replace(R.id.fragment_content, what, what.getClassTag()); 
    ft.commit(); 
    } 

R.id.fragment_content是一個FrameLayout裏,存儲我的片段(fragments)。 CustomFragmentPagerAdapter照常編碼。

回答

0

完成了改變

viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager())); 

viewPager.setAdapter(new CustomFragmentPagerAdapter(getChildFragmentManager()));