-1

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();
        }


    }
tccpg288
  • 3,242
  • 5
  • 35
  • 80

2 Answers2

1

You can simply done by getActivity.isFlag=true or flase Here isFlag is boolean in activity and you can access it by getActivity.

Harsh Shah
  • 74
  • 6
1
  1. Define an interface and abstract method in Fragment. If you having a click in Adapter then pass this delegate to the adapter or better have a click in Fragment.

CODE

public class MyFragment extends Fragment{
private Context mContext;

public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDelegate = (Delegate) mContext;
        ...
        ...
        ...
        onclick -> {
           mDelegate.passValue(value) 
         }

}

public interface Delegate{
 public void passValue(boolean value);

}

In your activity

public class MyActivity extends AppCompatActivity implements MyFragment. Delegate{

    @override    
    public void passValue(boolean value){
    } 

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Harsh
  • 599
  • 3
  • 20
  • 1
    When you include a code snippet in a list, you must add **8** spaces to the beginning of each line: 4 for the list item and 4 for the code. (Or you can just remove the "1." since you don't seem to be using the numbered list anyway.) – Code-Apprentice Aug 18 '18 at 02:41