You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ToggleButton调用setSelected()仅首次生效,后续外观不更新问题求助

Hey there! Let's work through this ToggleButton issue you're hitting in your AddEditTripFragment. I've dealt with similar state-update quirks in Fragments before, so here's what might be going wrong and how to fix it:

First: Double-check if you're using the right state method

ToggleButton's core "selected" UI state is actually tied to its checked property, not the generic View selected state. When you call setSelected() the first time, it might accidentally sync up with the initial checked state (making it look like it works), but subsequent calls won't trigger UI changes because the ToggleButton's default layout doesn't listen for changes to the selected flag.

Fix: Swap out setSelected() and isSelected() for setChecked() and isChecked() instead:

// Instead of this:
yourToggle.setSelected(true);
boolean isSelected = yourToggle.isSelected();

// Try this:
yourToggle.setChecked(true);
boolean isChecked = yourToggle.isChecked();

If you need to use the selected state (e.g., custom styling)

If you've customized your ToggleButton's appearance to depend on the View selected state, the problem might be that changing selected doesn't automatically trigger a UI redraw. Force the view to refresh after setting the state:

yourToggle.setSelected(newState);
yourToggle.invalidate(); // Forces the view to redraw with the new state

Check for stale View references

Fragments can rebuild their views (e.g., during screen rotations or configuration changes). If you're storing the ToggleButton as a member variable in your Fragment, you might be holding a reference to an old, discarded view instance—so when you call setSelected(), you're updating a view that's no longer on screen.

Fix:

  • Always fetch the ToggleButton from the current view instance when you need to modify it (instead of storing it long-term):
    // Inside any method where you need to update the ToggleButton:
    View currentView = getView();
    if (currentView != null) {
        ToggleButton toggle = currentView.findViewById(R.id.your_toggle_id);
        toggle.setSelected(newState);
    }
    
  • Or use ViewBinding to automatically handle fresh view references (this is my go-to for avoiding stale views):
    private FragmentAddEditTripBinding binding;
    
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentAddEditTripBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null; // Clean up to prevent memory leaks
    }
    
    // Later, access the ToggleButton like this:
    binding.yourToggleButton.setSelected(newState);
    

Restore state on Fragment rebuild

If your Fragment is being recreated (e.g., after rotation), you might lose the selected state unless you save and restore it manually:

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    ToggleButton toggle = getView().findViewById(R.id.your_toggle_id);
    outState.putBoolean("toggle_selected", toggle.isSelected());
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ToggleButton toggle = view.findViewById(R.id.your_toggle_id);
    if (savedInstanceState != null) {
        toggle.setSelected(savedInstanceState.getBoolean("toggle_selected"));
    }
}

Start with the first fix (switching to setChecked()) since that's the most common mistake with ToggleButtons. If you're using custom styling that relies on selected, move to the other solutions.

内容的提问来源于stack exchange,提问作者AndresReyMaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:08:28