Skip to content Skip to sidebar Skip to footer

Handle Togglebutton In Onresume()

I have written a program in which I am using Timer and controlling that timer using Toggle states. Toggle's default state is OFF, once I make changes in toggle state from OFF to ON

Solution 1:

"But problem starts when my Timer is ON and I switch to other activity and then again come back to Toggle activity"...

You DO not come back. As @Fabin Paul mentioned you just create a new instance of ToggleActivity. So in the scenario when you launch the app, then move to SwitchActivity and back by clicking the button, the back stack looks as follows:

ToggleActivity(1) -> SwitchActivity -> ToggleActivity(2)

"...and then do changes in toggle state from ON to OFF - it still runs Timer..."

You switch off the timer of the second instance of ToggleActivity. The one that is running belongs to the first ToggleActivity's instance.

"when I use finish(); or back press, in place of Intent to come back to ToggleActivity everything works fine..."

Yes, it does, because you don't create the second instance of ToggleActivity and your logic works correctly.

The easiest way to get the desired behavior is to add android:launchMode="singleInstance" to the ToggleActivity's tag of the manifest.

Solution 2:

I guess the problem is because for each instance of activity an instance of timer is created. So when you press stop only one instance of timer is stopped while the background activity's timer instance is not stopped.

I got around the problem by making timer static

static Timer timer;

and starting only one instance of the timer

publicvoidstartTimer() {
    if (timer == null) {
        timer = new Timer();
        initializeTimerTask();
        timer.schedule(timerTask, 1000, 5000);
    }
}

I hope it helps...

Solution 3:

When the ToggleActivity switches to onPause state, it doesn't cancel the running Timer. You need to cancel it manually.

You can add the following code snippet within your onPause() method:

if(timerTask!=null)timerTask.cancel();if(timer!=null) {
    timer.cancel();timer=null;
}

Solution 4:

here you are calling setChecked(true) means onCheckedChangeListener will call,then updating the preference also, you have to remove the listener and set listener to the toggle button,like this:

toggleMap.setOnCheckedChangeListener(null);
toggleMap.setChecked(isChecked);
toggleMap.setOnCheckedChangeListener(this);

EDIT

@OverrideprotectedvoidonResume() {
            super.onResume();

            tg1pref = preferences.getBoolean("tg1pref", false);
            toggleMap.setOnCheckedChangeListener(null);
            toggleMap.setChecked(tg1pref);
            toggleMap.setOnCheckedChangeListener(this);

               if (!tg1pref) {
                    if (timer != null) {
                         timer.cancel();
                          timer = null;
                        }   
                }
}

and

@OverrideprotectedvoidonPause() {
        toggleMap.setOnCheckedChangeListener(null);
        super.onPause();
    }

Solution 5:

Assign the listener to the toggle button after checking the state.

Just remove:

toggleMap.setOnCheckedChangeListener(this); 

line in onCreate and add the same line in onResume after updating the status.

toggleMap.setOnCheckedChangeListener(this); 

and in onPause():

toggleMap.setOnCheckedChangeListener(null);

like this:

@OverrideprotectedvoidonResume() {
    super.onResume();

    tg1pref = preferences.getBoolean("tg1pref", false);
    if (!tg1pref) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }   
    }
    toggleMap.setChecked(tg1pref);
    toggleMap.setOnCheckedChangeListener(this);
}

and:

@OverrideprotectedvoidonPause() {
    super.onPause();
    toggleMap.setOnCheckedChangeListener(null);
}

Post a Comment for "Handle Togglebutton In Onresume()"