2013-01-03 31 views
1

每當我第一次打開我的應用程序時,在我的xml文件中有3個容器中正確顯示了三個片段。在單擊操作欄上的選項卡時,其中一個容器的片段被換成另一個。應用程序重新啓動時的空白段落

的問題

每當打開該應用,交換到第二選項卡,使用返回箭頭退出該應用程序,然後重新打開該應用程序,並重新選擇第二個選項卡,該片段是空白。沒有出現,並且第三個選項卡也出現同樣的問題。爲什麼不顯示第二和第三個片段?

+1

向我們展示你的代碼(特別是你的背部方法和onresume,但也有其他你覺得很重要的東西) – AndroidPenguin

+0

@AndroidPenguin我設法解決了我的問題,但我非常感謝所有幫助。 – Adam

回答

0

我通過在每個片段中使用public static boolean started值來解決這個問題。在每個片段中的類,在onCreateView()我設置started = true;,並在onDestroyView()我設置started = false;

在我的主要活動onTabSelected()方法,我讀這boolean值,如果它是true,我附上片段回它的容器,但如果它是false,我創建了特定片段的新實例並將其添加到容器中。

+0

這是很好,你得到它的工作;請接受你自己的答案。但是,它看起來並不像是正確實施Tabs和碎片。您需要通過添加帶有標籤的片段,然後通過標籤接收片段。查看下面的答案,以查看正確實施選項卡的一種方式,或者從技術上講,正確地將ActionBar導航模式設置爲選項卡。 – ashokgelal

+0

這正是我實現標籤的方式。使用你的方法,碎片加載像正常,但我仍然有相同的問題發生碎片不會出現在第二次啓動,除非我從「最近的應用程序」中刪除應用程序。 – Adam

1
// from onCreate() method of your DefaultActivity class, call this method: 

// file: DefaultActivity.java 

... 
... 
... 
    private void addTabs() { 
     // get support ActionBar and set navigation mode to Tabs 
     ActionBar bar = getSupportActionBar(); 
     bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Add first tab to ActionBar, and set the TabListener to a new TabListner object 
     String title1 = "first_tab"; 
     ActionBar.Tab tab1 = bar.newTab(); 
     tab1.setText(title1); 
     tab1.setTabListener(new TabListener(this, title1, Fragment1.class)); 
     bar.addTab(tab1); 

     // Add second tab to ActionBar, and set the TabListener to a new TabListner object 
     String title2 = "second_tab"; 
     ActionBar.Tab tab2 = bar.newTab(); 
     tab2.setText(locationsTitle); 
     tab2.setTabListener(new TabListener(this, tab2, Fragment2.class)); 
     bar.addTab(tab2); 

     // Add third tab to ActionBar, and set the TabListener to a new TabListner object 
     String title3 = "third_tab"; 
     ActionBar.Tab tab3 = bar.newTab(); 
     tab3.setText(title3); 
     tab3.setTabListener(new TabListener(this, title3, Fragment3.class)); 
     bar.addTab(tab3); 
    } 
... 
... 
... 

現在,你需要箱子三個片段 - 片段1,Fragment2和Fragment3以及創建TabListener:

// file: TabListener.java 

public class TabListener implements ActionBar.TabListener { 

    private final FragmentActivity mActivity; 
    private final String mTag; 
    private final Class mFragmentClass; 
    private Fragment mFragment; 

    public TabListener(FragmentActivity activity, String tag, Class fragmentClass) { 
     mActivity = activity; 
     mTag = tag; 
     mFragmentClass = fragmentClass; 
     // see if we already have the fragment with given tag. it's okay if it is null 
     mFragment = activity.getSupportFragmentManager().findFragmentByTag(tag); 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
     if (mFragment == null) { 
      // instantiate a new fragment for the given class 
      mFragment = SherlockFragment.instantiate(mActivity, mFragmentClass.getName()); 
      // place in the default root viewgroup - android.R.id.content 
      ft.replace(android.R.id.content, mFragment, mTag); 
     } else { 
      if (mFragment.isDetached()) 
       ft.attach(mFragment); 
     } 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      ft.detach(mFragment); 
     } 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 
    } 
} 

這就是你需要的。