I have a Fragment that implements a ClickListener. The hosting activity either displays a blank fragment, or it displays, in the fragment container (FrameLayout) the values of the item clicked.
I am handling rotation changes, and I need to know if an item has been clicked in the fragment. If it has been clicked, I want to display the data on all orientation changes. If nothing has been clicked, I want to display the blank fragment that has a simple step that says "please click."
I simply need to know how to pass this boolean back to the activity, I believe an interface is an option but need some coding help:
Adapter, which dictates what happens when item is clicked in Fragment:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction transaction = mFragmanager.beginTransaction();
if (position == 0){
IngredientsListFragment ingredientsListFragment = IngredientsListFragment.newInstance(mRecipeList, mRecipePosition, position, mIngredientsDataSet);
RecipeStepsFragmentTwo recipeStepsFragmentTwo = (RecipeStepsFragmentTwo) mFragmanager.findFragmentById(R.id.recipe_details_two);
transaction.remove(recipeStepsFragmentTwo);
transaction.add(R.id.recipe_details_three, ingredientsListFragment);
transaction.commit();
} else {
RecipeStepsFragmentThree recipeStepsFragmentThree;
recipeStepsFragmentThree = RecipeStepsFragmentThree.newInstance(mRecipeList, mRecipePosition, (position -1));
//TODO: Why am I being prompted for V4? Am I handling correctly?
//TODO: Am I handling removal of previous frag correctly?
RecipeStepsFragmentTwo recipeStepsFragmentTwo = (RecipeStepsFragmentTwo) mFragmanager.findFragmentById(R.id.recipe_details_two);
transaction.remove(recipeStepsFragmentTwo);
transaction.add(R.id.recipe_details_three, recipeStepsFragmentThree);
transaction.commit();
}
}
});
Hosting Activity:
if (savedInstanceState == null) {
RecipeStepsFragmentTwo recipeStepsFragmentTwo = RecipeStepsFragmentTwo.newInstance(mRecipeList, mPosition);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.recipe_details_two, recipeStepsFragmentTwo)
.commit();
//two-pane
if (findViewById(R.id.two_pane_constraint_layout) != null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTwopane = true;
BlankFragment blankFragment = new BlankFragment();
fm.beginTransaction()
.add(R.id.recipe_details_three, blankFragment)
.commit();
}
}
else {
mRecipeList = savedInstanceState.getParcelableArrayList(RECIPE_LIST);
mPosition = savedInstanceState.getInt(RECIPE_POSITION);
FragmentManager z = getSupportFragmentManager();
//check if fragment was clicked, if so no longer necessary to show blank
RecipeStepsFragmentThree recipeStepsFragmentThree = (RecipeStepsFragmentThree) z.findFragmentById(R.id.recipe_details_three);
if (recipeStepsFragmentThree != null){
return;
}
BlankFragment blankFragment = (BlankFragment) z.findFragmentById(R.id.recipe_details_three);
if (blankFragment != null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
z.beginTransaction().remove(blankFragment).commit();
}
if (blankFragment == null & getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
BlankFragment newLandscapeFrag = new BlankFragment();
z.beginTransaction().add(R.id.recipe_details_three, newLandscapeFrag).commit();
}
}