ListFragment に関連付けられた ArrayList を保存する必要があります。私のワークフローは次のとおりです。
Activity1 [フラグメント 1 - ArrayList1]
onListItemClick();
Activity1 [フラグメント 1 - ArrayList2]
onListItemClick();
アクティビティ2
戻る(); // 戻るボタンまたはアクションバーのアイコンで戻る
Activity1 [フラグメント 1 - ArrayList2]
戻る();
Activity1 [フラグメント 1 - ArrayList1]
戻る();
出口
ArrayList はどこに保存すればよいですか? アクティビティホストまたはフラグメント内?
ホームアクティビティ:
public class HomeActivity extends ActionBarActivity implements
CategoriesListFragment.OnCategorySelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
CategoriesListFragment fragment = new CategoriesListFragment();
fragmentTransaction.add(R.id.linear_home, fragment);
// Should I retrieve the ArrayList here?
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
@Override
public void onCategorySelected(String category) {
// Could this be somehow useful?
}
}
CategoriesListFragment: //.................................................
public class CategoriesListFragment extends ListFragment {
private ArrayAdapter<String> adapter;
private OnCategorySelectedListener mListener;
String[] categories = new String[] { "Category 1", "Category 2", "Category 3", "Category 4",
"Category 5", "Category 6", "Category 7", "Category 8", "Category 9" };
String[] subCategories = new String[] { "Subcategory 1", "Subcategory 2", "Subcategory 3",
"Subcategory 4", "Subcategory 5", "Subcategory 6", "Subcategory 7", "Subcategory 8", "Subcategory 9",
"Subcategory 10", "Subcategory 11" };
ArrayList<String> categoriesList;
ArrayList<String> subCategoriesList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_categories,
container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Cast is just useful to use ArrayList helper methods
categoriesList = new ArrayList<String>();
categoriesList.addAll(Arrays.asList(categories));
subCategoriesList = new ArrayList<String>();
subCategoriesList.addAll(Arrays.asList(subCategories));
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, categoriesList);
setListAdapter(adapter);
}
public interface OnCategorySelectedListener {
public void onCategorySelected(String category);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnCategorySelectedListener) activity;
} catch (ClassCastException cce) {
throw new ClassCastException(activity.toString()
+ "must implement OnCategorySelectedListener");
}
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
adapter.clear();
// addAll() >= API level 11, I'm targetting API 7
for (int i = 0; i < subCategoriesList.size(); i++)
adapter.add(subCategoriesList.get(i));
adapter.notifyDataSetChanged();
// Scrolling to top after click
listView.setSelection(0);
mListener.onCategorySelected(categories[position]);
}
}
最後のメソッド onListItemClick は、最初のクリックでは問題ありませんが、戻ってきたとき、リストビューは明らかに空です。カテゴリの ArrayList を保存するためのコードをどのように構築するのか、私は悩んでいます。どんな助けでも大歓迎です。どうもありがとう。私は意図的に XML ファイルを省略しました。