我有一個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。這裏的情景,
我有自定義相機的實施,從片段我打開我的自定義相機(不破壞片段或活動),捕獲圖像,我回來片段
onCreateView()
有時也被稱爲,所以sectionView
將變爲空。當我調試時,不會調用任何生命週期方法(例如onDestroyView()
,onDestroy()
,onDetach()
,onLowMemeory()
, )。那麼如何調用onCreateView()
?在Nexus 9中,在更改運行時權限時發生了同樣的情況。我將會在
Fragment
,向下滾動狀態欄>轉到設置>應用程序>應用權限>位置>撤銷位置權限。如果我的用戶授予權限它不會崩潰,但如果我撤銷sectionView對象將是無效
怪異的行爲:要檢查2次的情況下,我與調試,以確保片段/活性被破壞或者撤銷Location
權限。 一旦我撤銷了位置權限,調試器就斷開連接,應用程序被終止/死機。這發生在我撤銷任何權限時。
現在我怎樣才能確保sectionView
對象被保留或不被破壞?
謝謝你的答案,讓我在這個工作,讓你知道 –
Okie,我看你的答案。但我有一個問題,正如我所提到的'SectionView'是一個** AbstractClass **。我在許多自定義視圖類中擴展了這個抽象類。如果我讓'SectionView'類實現'Serializable'接口,那麼我不得不讓所有其他擴展的類也實現'Serializable'嗎?如果我不這樣做,那麼它會拋出'NotSerializableException'!例如:'引起:java.io.NotSerializableException:android.support.v7.widget.AppCompatEditText'。在我的一個自定義視圖中有'AppCompatEditText'。我應該怎麼做? –
我剛剛編輯了關於抽象類部分的小問題。我的抽象類被另一個類添加自定義視圖的類擴展。所以我不認爲這會有所幫助。我想我必須重新構建。但歡迎任何建議或解決方案... –