一般的な質問 フラグメンテーションをシングルトンとして定義できますか?私のアプリケーションでAndroidのシングルトンとしてのフラグメント
具体的な質問 私は2つのフラグメント、FragmentA
とFragmentB
を持ってFragmentPager
と1「FragmentActivity」を持っています。
FragmentA
でシングルトンがFragment
クラス延びるにつれて、私は断片を定義した:
private static instance = null;
public static FragmentA getInstance() {
if (instance == null) {
instance = new FragmentA();
}
return instance;
}
private FragmentA() {}
と私のFragmentPagerAdapter
中を:
@Override
public Fragment getItem(int position) {
switch(position){
Fragment fragment = null;
case 0:
fragment = FragmentA.getInstance();
break;
case 1:
fragment = FragmentB.getInstance();
break;
}
return fragment;
}
、これは私がフラグメントレイアウトを膨らませる方法です:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView = (RelativeLayout) inflater.inflate(R.layout.fragment_a_layout, container, false);
return fragmentView;
}
私の問題:
初めてアプリケーションを起動すると、すべてうまく動作します。 アプリケーションを閉じてから再起動すると、両方のフラグメントが表示されません。
どうしたらいいですか?私はそれがエラーheheだと信じています..私のうまく働いて – Sheychan