2

我有一個Activity它有ViewPager。這ViewPager加載Fragments使用Adapter。我的每個片段都被視爲部分(根據應用功能)。起初,我加載所有ViewPager項目一旦使用,片段不被破壞,但onCreateView被稱爲

viewPager.setOffscreenPageLimit(sectionList.size()); //sectionList is nothing but number of framgents

在片段我有一個名爲setup在創建片段從Adapter類調​​用的函數。

InvestigationFragment

public void setup(SectionView sectionView, int pos) { 
    this.sectionView = sectionView; 
    this.pos = pos; 
} 

InvestigationAdapter

@Override 
public Fragment getItem(int pos) { 
    InvestigationFragment investigationFragment = new InvestigationFragment(); 
    investigationFragment.setup(sectionViewHashMap.get(keys.get(pos)), pos); 
    fragments.add(investigationFragment); 
    return investigationFragment; 
} 

這裏SectionView是一個抽象類由SimpleSection類延長。並且SimpleSection在其中添加自定義視圖。自定義視圖就像20-30個視圖類。

public abstract class SectionView { 

    public Section mSection; 
    public Context mContext; 
    public LinkedHashMap<Integer, SectionView> subSectionViews; 
    public LinkedHashMap<Integer, FieldWidget> fieldWidget; 
    public int actionSaveId; 
    protected LinearLayout sectionView; 
    protected Toolbar toolbar; 

    public final void initialiseSection(Toolbar toolbar) { 
     this.toolbar = toolbar; 
     createFields(); 
     createSubSection(); 
    } 

    public final void buildSection(LinearLayout v) { 
     sectionView = v; 
     addFields(); 
     addSubSection(); 
    } 

    protected abstract void createFields(); 
    protected abstract void createSubSection(); 
    protected abstract void addFields(); 
    protected abstract void addSubSection(); 
    public abstract boolean hasSubSection(); 
    public abstract boolean validateSection(); 
    protected abstract boolean persistSection(); 

    public final boolean saveSection() { 
     boolean isSaved = true; 
      if (!validateSection()) { 
       isSaved = false; 
      } else if (!persistSection()) { 
       isSaved = false; 
      } 
     return isSaved; 
    } 
} 

一切都很好,直到在這裏。但是在某些我無法保留它的情況下,SectionView會變爲null。這裏的情景,

  1. 我有自定義相機的實施,從片段我打開我的自定義相機(不破壞片段或活動),捕獲圖像,我回來片段onCreateView()有時也被稱爲,所以sectionView將變爲空。當我調試時,不會調用任何生命週期方法(例如onDestroyView()onDestroy()onDetach(),onLowMemeory(), )。那麼如何調用onCreateView()

  2. 在Nexus 9中,在更改運行時權限時發生了同樣的情況。我將會在Fragment,向下滾動狀態欄>轉到設置>應用程序>應用權限>位置>撤銷位置權限。如果我的用戶授予權限它不會崩潰,但如果我撤銷sectionView對象將是無效

怪異的行爲:要檢查2次的情況下,我與調試,以確保片段/活性被破壞或者撤銷Location權限。 一旦我撤銷了位置權限,調試器就斷開連接,應用程序被終止/死機。這發生在我撤銷任何權限時。

現在我怎樣才能確保sectionView對象被保留或不被破壞?

回答

0

有幾件事。您可以將您的離屏頁面限制設置爲size()-1,因爲其中一個總是在屏幕上。

而不是在你的片段上有一個設置方法,我建議用你想要的值實例化你的片段。因此在InvestigationFragment中創建一個名爲newInstance的靜態方法,它接受SectionView和Position參數。

E.g.

public static InvestigationFragment newInstance(SectionView sectionView, int pos) { 
    final InvestigationFragment fragment = new InvestigationFragment(); 
    final Bundle args = new Bundle(); 
    args.putSerializable("SectionView", sectionView); //Depends on what type this as to what the put method is 
    args.putInt("Position", position); 
    fragment.setArguments(args); 
    return fragment; 
} 

然後,您應該將這些值讀回到InvestigationFragment的onCreate中。

sectionView = (SectionView) getArguments().getSerializable("SectionView"); 

這應該讓你的InvestigationFragment正常生存旋轉變化等。

在適配器類我建議只是在做:

@Override 
    public Fragment getItem(final int position) { 
     return InvestigationFragment.newInstance(sectionViewHashMap.get(keys.get(position)), position); 
} 

如果你真的需要保持片段的列表中您的尋呼機適配器,你應該重寫instantiateItem和destroyItem如下:

@Override 
public Object instantiateItem(final ViewGroup container, final int position) { 
    final InvestigationFragment fragment = (InvestigationFragment) super.instantiateItem(container, position); 
    fragments.put(position, fragment); 
    return fragment; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    fragments.remove(position); 
    super.destroyItem(container, position, object); 
} 
+0

謝謝你的答案,讓我在這個工作,讓你知道 –

+0

Okie,我看你的答案。但我有一個問題,正如我所提到的'SectionView'是一個** AbstractClass **。我在許多自定義視圖類中擴展了這個抽象類。如果我讓'SectionView'類實現'Serializable'接口,那麼我不得不讓所有其他擴展的類也實現'Serializable'嗎?如果我不這樣做,那麼它會拋出'NotSerializableException'!例如:'引起:java.io.NotSerializableException:android.support.v7.widget.AppCompatEditText'。在我的一個自定義視圖中有'AppCompatEditText'。我應該怎麼做? –

+0

我剛剛編輯了關於抽象類部分的小問題。我的抽象類被另一個類添加自定義視圖的類擴展。所以我不認爲這會有所幫助。我想我必須重新構建。但歡迎任何建議或解決方案... –