rsd_unleashed
rsd_unleashed

Reputation: 161

Communicating between Fragment and Activity

I have a fragment with multiple toggle buttons. On click of a particular button in my Activity, I'd like to upload the status of all these toggle buttons to the server. I see there is one way of doing it as explained in the below link, where we create a listener interface and on every click of each toggle button, we update some tag/integer in the activity corresponding to each toggle button.

https://developer.android.com/training/basics/fragments/communicating.html

But I would like to know if there is any way to know the checked/unchecked status of all toggle buttons in the fragment from the activity without implementing the interface methods for each time a toggle button is clicked. Hope I'm clear.

Upvotes: 1

Views: 963

Answers (2)

Tharkius
Tharkius

Reputation: 3518

Here's a working example of how to achieve that. The activity:

public class MainActivity extends AppCompatActivity {
    ToggleFragment toggleFragment;
    Button updateStatusButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);

        updateStatusButton = (Button) findViewById(R.id.updateStatusButton);

        toggleFragment = ToggleFragment.newInstance();

        getSupportFragmentManager().beginTransaction()
                                    .replace(R.id.main_frame, toggleFragment, "ToggleFragment")
                                    .commit();

        updateStatusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean buttonsChecked = toggleFragment.areToggleButtonsChecked();

                Toast.makeText(MainActivity.this, String.format("Toggle buttons checked: %s", buttonsChecked), Toast.LENGTH_LONG).show();
            }
        });
    }
}

And the Fragment:

public class ToggleFragment extends Fragment {
    ToggleButton toggleButton1;
    ToggleButton toggleButton2;
    ToggleButton toggleButton3;

    public static ToggleFragment newInstance() {
        Bundle args = new Bundle();
        ToggleFragment fragment = new ToggleFragment();

        fragment.setArguments(args);

        return fragment;
    }

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

        toggleButton1 = (ToggleButton) view.findViewById(R.id.toggleButton1);
        toggleButton2 = (ToggleButton) view.findViewById(R.id.toggleButton2);
        toggleButton3 = (ToggleButton) view.findViewById(R.id.toggleButton3);

        return view;
    }

    public boolean areToggleButtonsChecked() {
        return toggleButton1.isChecked()
                && toggleButton2.isChecked()
                && toggleButton3.isChecked();
    }
}

Upvotes: 1

WoogieNoogie
WoogieNoogie

Reputation: 1257

Honestly, the best way that you could accomplish this is with a ViewModel from the Android Architecture Components library. The ViewModel can have an ObservableField or LiveData value that each of the Fragments can observe, and always have the correct information. This would also make it easier to be lifecycle-aware.

Upvotes: 0

Related Questions