I am running into a problem when I try to reassign an OnClickListener. In the code below, my program runs through the startWorkout() method and each time the button is clicked it calculates whether this is the last run through or not. If it is the last run through, it calls the lastSet() method. The lastSet() method is supposed to reasssign the button so that it does a different action but everytime I click it, it does the stuff declared in the startWorkout() method.
When I ran it through a debug mode, stepping through, the program correctly went through the lastSet() method and assigned the listener. But then it went back to the startWorkout() method and reassigned the listener! So by the time a user clicked it, the button was back to its original listener instead of the final listener.
Does anyone know why this is happening?
public void startWorkout() {
doSet();
Button doneButton = (Button)findViewById(R.id.done);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If this is the last result, calls last(set) instead
saveResult();
takeRest(61000, 30000);
}
});
}
public void lastSet() {
// Changes layout (keeps the same button)
setLayoutTest();
Button doneButton = (Button)findViewById(R.id.done);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call Finish Methods
}
});
}
Solved it…I was actually assigning the listener twice… (solved a long time ago, just cleaning up my stackoverflow questions)
If you do this:
and then this:
then ONLY the second one (runMethod2() in this case) will run. Rookie mistake, just be sure to delete (or overwrite) any legacy code.