Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change ToggleButton state programmatically?

I have a ToggleButton defined like this:

<ToggleButton android:text="ToggleButton" android:id="@+id/toggle"
    android:layout_height="wrap_content" android:layout_width="fill_parent"></ToggleButton>

And I want to change its state programmatically. I tried using the setChecked and toggle methods, but neither of those works in my situation.

I have an ongoing notification and when my activity receives the notification intent, the toggle button should be set to not checked, but it's not working.

I'm doing this on the activity's onResume. The code is being executed but the ToggleButton's state doesn't change.

Weirdly, if I call setChecked(false) on the activity's onCreate it does changes the button's state, but not on onResume. What am I missing?

Thanks.

like image 923
Franco Avatar asked May 09 '11 21:05

Franco


1 Answers

Got it. Kind of.

I had this

    protected void onResume() {
        super.onResume();

        Intent intent;

        if ((intent = getIntent()) != null && MainActivity.STOP.equals(intent.getAction())) {
            disable();

            toggle.setChecked(false);

            finish();
        }
    }

But the call to finish wasn't actually doing anything. I removed it and now it works. Not a clue why this fixed it.

Someone care to explain?

like image 74
Franco Avatar answered Oct 04 '22 15:10

Franco