バックスタックの使用時に UI の状態を保持することについて質問があります。
シナリオ
setRetainInstance(true); で 2 つのフラグメントが使用されます。そしてそれらの ui は onActivityCreated() で作成されます。1 つのフラグメントには、バックスタック経由で戻ってきたり、別のアプリに変更したりするときに、スクロール位置を保持する必要がある GridView があります。
問題:
ホームを押してアプリケーションに戻ると、スクロール位置を保持するいくつかの項目を持つ GridView があります。
しかし、スクロールして別のフラグメントに移動し、戻るボタンで戻ると、UI が再作成されます (scrollpos は保持されません)。
各状態を手動で保存および復元せずにバックスタックを使用するときに ui 状態を保持するには、どうすればよいですか?
アップデート
原則を知りたくないだけなので、ここでは私のコードは重要ではないと思います。バックスタックを介して別のアクティビティに戻るため、私のコードはすでにアクティビティで正常に動作していますが、フラグメントのみが動作しません。ホームを押してアプリを再度起動すると、このフラグメントでもすべてが期待どおりに保持されます。
メインフラグメント:
public class ThumbnailFragment extends Fragment implement IView {
GlobalModel model;
ThumbnailController controller;
final ApplicationDataHolder appDataHolder = new ApplicationDataHolder().getInstance();
PreferencesProvider preferences;
Button datePicker;
Spinner newspaperEditionSelector;
TextView newspaperEditionViewer;
GridView thumbnailView;
ImageSwitcher nothingLoaded;
int newspaperEditionPosition = -1;
String newspaperDate = null;
protected Adapter initializedAdapter = null;
@Override
public void updateView() {
newspaperEditionSelector = (Spinner) getActivity().findViewById(R.id.newspaperEditionSelector);
newspaperEditionViewer = (TextView) getActivity().findViewById(R.id.newspaperEditionViewer);
thumbnailView = (GridView) getActivity().findViewById(R.id.thumbnailFragment_thumbnailList);
datePicker = (Button) getActivity().findViewById(R.id.choosenDate);
nothingLoaded = (ImageSwitcher) getActivity().findViewById(R.id.nothingLoaded);
if (newspaperEditionSelector == null || newspaperEditionViewer == null || thumbnailView == null || datePicker == null || nothingLoaded == null)
return;
if (model.getSubscriptions() == null || model.getSubscriptions().size() < 1) {
Bundle args = new Bundle();
args.putBoolean("promptForLoginData", true);
MainActivity.showFragment(getActivity(), getFragmentManager(), SettingsFragment.class.getName(), args, true);
} else {
if (model.getSubscriptions().size() > 1) {
// Use spinner for user to select desired edition
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, model.getSubscriptions());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newspaperEditionSelector.setAdapter(dataAdapter);
newspaperEditionSelector.setSelection(newspaperEditionPosition);
this.newspaperEditionSelector.setVisibility(View.VISIBLE);
this.newspaperEditionViewer.setVisibility(View.GONE);
} else {
// because only one edition for user exists use textview instead
// of spinner
this.newspaperEditionViewer.setText(model.getSubscriptions().get(0));
this.newspaperEditionViewer.setVisibility(View.VISIBLE);
this.newspaperEditionSelector.setVisibility(View.GONE);
preferences.setIntegerPreference("ChoosenThumbnailNewspaperEdition", 0);
}
// Create on item choose listener
newspaperEditionSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long arg3) {
if (initializedAdapter != parent.getAdapter()) {
initializedAdapter = parent.getAdapter();
return;
} else {
newspaperEditionPosition = (int) parent.getSelectedItemId();
preferences.setIntegerPreference("ChoosenThumbnailNewspaperEdition", newspaperEditionPosition);
controller.loadNewspaper(newspaperDate, model.getSubscriptions().get(newspaperEditionPosition));
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// Prepare date picker ui
datePicker.setText(newspaperDate);
datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
datePicker.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
//controller.loadNewspaper(newspaperDate, model.getSubscriptions().get(newspaperEditionPosition));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
String title = "";
if (model.getThumbnailsAdapter() == null) {
nothingLoaded.setVisibility(View.VISIBLE);
title += "...";
} else {
nothingLoaded.setVisibility(View.GONE);
title += "Zeitung - " + model.getSubscriptions().get(newspaperEditionPosition) + (newspaperDate != null ? " von " + newspaperDate : "");
}
getActivity().getActionBar().setTitle(title);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
preferences = new PreferencesProvider(getActivity());
this.newspaperEditionPosition = preferences.getIntegerPreference("ChoosenThumbnailNewspaperEdition");
this.newspaperDate = (preferences.getStringPreference("ChoosenThumbnailNewspaperDate").equals("") ? "Aktuell" : preferences.getStringPreference("ChoosenThumbnailNewspaperDate"));
model = new GlobalModel().getInstance();
model.init(getActivity().getApplicationContext());
model.registerView(new WeakReference<IView>(this));
controller = new ThumbnailController(model, this.getActivity());
controller.loadSubscriptions();
if (model.getSubscriptions() != null)
controller.loadNewspaper(newspaperDate, model.getSubscriptions().get(newspaperEditionPosition));
thumbnailView.setAdapter(model.getThumbnailsAdapter());
thumbnailView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Change fragment to pageview
Bundle args = new Bundle();
args.putString("newspaperEdition", model.getSubscriptions().get(newspaperEditionPosition));
args.putString("newspaperDate", newspaperDate);
args.putInt("newspaperPagePosition", position + 1);
args.putInt("newspaperPagesCount", parent.getAdapter().getCount());
MainActivity.showFragment(getActivity(), getFragmentManager(), PageFragment.class.getName(), args, true);
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_thumbnail, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Clear and replace menu in actionbar and hide listbox
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(R.string.title_thumbnail_fragment);
actionBar.setDisplayHomeAsUpEnabled(false);
menu.clear();
inflater.inflate(R.menu.thumbnail_fragment_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_goto_search:
MainActivity.showFragment(getActivity(), getFragmentManager(), SearchFragment.class.getName(), null, true);
break;
case R.id.menu_export_pdf:
NewspaperDownloader task = new NewspaperDownloader(getActivity(), model.getSubscriptions().get(newspaperEditionPosition), this.newspaperDate, -1);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
break;
case R.id.menu_settings:
MainActivity.showFragment(getActivity(), getFragmentManager(), SettingsFragment.class.getName(), null, true);
break;
case R.id.menu_exit:
getActivity().finish();
System.exit(0);
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
}