0

我目前正在使用其中一個片段(主導航是NavigationDrawer)中的ActionBar選項卡的應用程序。片段的onCreate()ViewPager/FragmentPageAdapter,重新創建後,不會調用最後所選視圖的onCreate()

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    actionBar = activity.getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Resources r = getResources(); 
    //(…) tabNames initialization 

    adapter = new CustomTabAdapter(getFragmentManager()); 
} 

onCreateView()

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment, container, false); 
    viewPager = (ViewPager) rootView.findViewById(R.id.pager); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(this); 

    for (String tabName : tabNames) { 
     actionBar.addTab(actionBar.newTab() 
       .setText(tabName) 
       .setTabListener(this)); 
    } 
    return rootView; 
} 

共有4個標籤,他們每個人都有自己的片段和佈局。 CustomTabAdapter在其getItem(int)中返回適當片段的新實例。有4個選項卡。

現在我的問題是:

  1. 我啓動應用程序。
  2. 我從NavigationDrawer列表中選擇此選項卡片段。一切都很好。
  3. 我從NavigationDrawer列表中選擇另一個片段,這意味着選項卡片段被替換爲它。
  4. 我再次選擇tab片段。與從NavigationDrawer列表中選擇另一個片段之前選擇的選項卡相關的片段以及與其相鄰的片段不會重新創建(ActionBar選項卡下的空白屏幕)。我檢查和onCreateView(…)這些片段的方法不叫。因此,在更改設備方向後,或者選擇不相鄰的選項卡,並再次顯示此適當的佈局。

我該如何使它工作,因爲它應該(顯示適當的佈局,重新從NavigationDrawer列表中的標籤片段,而不是空白)?我用完了想法。

回答

1

最後,我找到了一個解決方案。我的CustomTabAdapterFragmentPagerAdapter的擴展。我將它改爲FragmentStatePageAdapter的擴展名,現在重新創建了片段。

更多詳情請登錄@Louth的this answer

0

它需要從緩存沒有休閒片段

+0

我想,但我該如何清除緩存?我想要這些片段被重新創建。 – Tiero

相關問題