Parcelable을 Bundle로 내 Activity에서 Fragment로 확장하는 사용자 정의 POJO를 전달하려고합니다. 채워진 번들 객체는 내 Fragment의 newInstance 메소드에 성공적으로 전달됩니다. 그러나 내 Fragment의 onCreate 메서드에서이 POJO 클래스의 ArrayList를 복구하려고하면 어떻게 든 삭제됩니다.Android : Parcelable을 Activity에서 Fragment로 확장하는 Custom POJO의 ArrayList 전달
//Activity
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
mRecipe = getIntent().getParcelableExtra("com.wernerraubenheimer.bakingapp.data.RecipeModel");
//This works mRecipe is successfully populated with my custom ArrayList<IngredientModel>
//My class IngredientModel extends Parcelable
IngredientListFragment ingredientListFragment = IngredientListFragment.newInstance(mRecipe.getIngredients());
//Still good so far....
getSupportFragmentManager()
.beginTransaction()
.add(R.id.first_fragment_container, new IngredientListFragment())
.commit();
}
//Fragment
public static IngredientListFragment newInstance(ArrayList<IngredientModel> ingredients) {
IngredientListFragment ingredientListFragment = new IngredientListFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("ingredients", ingredients);
ingredientListFragment.setArguments(bundle);
return ingredientListFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This is where the ball gets dropped, mIngredientList which I initialized when I declared it just after
//class declaration: private ArrayList<IngredientModel> mIngredientList = new ArrayList<>();
//is now empty??
//I have also placed the call to super.onCreate after the statement below
mIngredientList = getArguments().getParcelableArrayList("ingredients");
//have also used savedInstanceState
}
나는 나의 조각의 onCreateView에 RecyclerView에 ArrayList를 사용하려는 :
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ingredient_list, container, false);
rvIngredientList = (RecyclerView)rootView.findViewById(R.id.rv_ingredient_list);
ingredientLayoutManager = new LinearLayoutManager(getActivity());
ingredientLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvIngredientList.setLayoutManager(ingredientLayoutManager);
IngredientListAdapter ingredientListAdapter = new IngredientListAdapter(mIngredientList);
rvIngredientList.setAdapter(ingredientListAdapter);
return rootView;
}
이 오류는 모범적 인 오류입니다. – Werner
이 답을 올바른 것으로 표시하면 좋을 것입니다. @werner – rajatgoyal715