-1

I have an activity that is the foundation of 2 fragments. The activity chooses which fragment to show based on a boolean. Currently, I have set the boolean state individually to the activity and the fragments like so:

Activity:

    public boolean setupCompleted = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
        {...}     

    if (setupCompleted){
        setFragment(programFragment);
    } else {
        setFragment(setupFragment);
    }

}

setupFragment:

   public boolean setupCompleted = false;

public SetupFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.setup, container, false);

    pButton = view.findViewById(R.id.pButton);

    pButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setupCompleted = true;
            Toast.makeText(getActivity(), "Boolean state changed to True!", Toast.LENGTH_LONG).show();
        }
    });

    return view;
}

The architecture is simple, The Activity starts and shows the setupFragment right away, but when you complete the setup the Activity will show the programFragment.

Now my problem lies in the fact that I want the button to change the boolean to true so that the main Activity can show the programFragment but I was not able to find any simple explanation on the internet in how to get the boolean state from the setupFragment.

Later I will save the instance state so that the setupFragment never shows again to a user that already completed the setup.

newbieCoder.pkg
  • 277
  • 2
  • 14
  • 1
    Possible duplicate of [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – FarshidABZ May 26 '18 at 11:38

2 Answers2

1

Use interface

interface fragmentListener{
   void onComplete();
}

class Activity implements fragmentListener{

  protected void onCreate(Bundle savedInstanceState) {
    setFragment(new setupFragment(this));
  }

  @Override
  public void onComplete(){
   //code here...
  }
}

class setupFragment{
  setupFragment(fragmentListener fl){

  }
}
0

For this scenario you need to communicate between Activity and Fragment.

You can do that via Interface . Define a Interface in Fragment

    // Container Activity must implement this interface
public interface OnChnageEvent {
    public void updateCompletedStatus(int value);
}

In Fragment :

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnChnageEvent) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnChnageEvent");
    }
}

When some event happens call

 if(mCallback != null){
    //Send the event to the host activity
    mCallback.onArticleSelected(position);
 }

Implement Interface in Activity

public class MainActivity extends AppCompatActivity implements SetupFragment.OnChnageEvent{
  ...

  public void updateCompletedStatus(int value) {

     // Do something here 
     // Change your Fragment 
  }
}

For more see Official Tutorial

The best approach would be to use ViewModel of Architecture Component. Check here to have a look. You can communicate between Activity, Fragment via ViewModel

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50