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.