【发布时间】:2014-04-22 11:13:54
【问题描述】:
在一个 android 应用程序中,我有多个片段,在一个片段(片段 A)中,我使用布尔变量在按钮单击时在 GridView 中的两组图像之间切换。 Th GridView 项目单击导致第二个片段(片段 B)。在 Fragment B 上按下 Back 按钮时,Fragment A 中的 gridview 会加载默认的图像集,而不是在继续下一个 Fragment Fragment B 之前设置的图像。
请指导如何恢复以前的图像集而不是默认图像集?
在 MainActivity 中,我在 BackPressed 上实现如下:
@Override
public void onBackPressed() {
if (frag instanceof SettingsFragment)
loadGridViewFragment();
else if (frag instanceof SingleDuaViewPagerFragment) {
boolean keyIsFavSel = new SharedPreferencesSupplication().read(SingletonClass.keyIsFavSelected, false);
if (keyIsFavSel)
loadDuasListFragment();
else
loadGridViewFragment();
}
else if (frag instanceof DuasListFragment) {
boolean keyIsFavSel = new SharedPreferencesSupplication().read(SingletonClass.keyIsFavSelected, false);
if (keyIsFavSel)
loadGridViewFragment();
else
loadSingleDuaFragment();
}
else
super.onBackPressed();
}
GridViewFragment.java的OnCreate方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// fragment = new GridViewFragment();
context = inflater.getContext();
View view = inflater.inflate(R.layout.fragment_gridview, container, false);
ga = new GridViewAdapter(context);
Btn_lang_Ch = (Button) view.findViewById(R.id.lng_ch);
Btn_Settings = (Button) view.findViewById(R.id.button_settings);
favoriteDuas = (Button) view.findViewById(R.id.btn_favorite_duas);
GridMenu =(GridView) view.findViewById(R.id.gridView1);
float scalefactor = getResources().getDisplayMetrics().density * 150;
@SuppressWarnings("deprecation")
int number = getActivity().getWindowManager().getDefaultDisplay().getWidth();
int columns = (int) ((float) number / (float) scalefactor);
GridMenu.setAdapter(ga);
GridMenu.setNumColumns(columns);
setRetainInstance(true);
return view;
}
【问题讨论】:
标签: android gridview android-fragments savestate