Search code examples
androidonclicklistenerandroid-custom-viewbutterknife

ButterKnife OnClick listeners runs shortly after view created in custom view


I have a strange problem with ButterKnife's OnClick listeners. When my custom view opened below OnClick listeners are running automatically when view opened. Below code helps you to understand what my problem is.

public class UserMenuFragment extends LinearLayout {
@BindView(R.id.buttonBlockUser)
Button buttonBlockUser;
@BindView(R.id.buttonMuteUser)
Button buttonMuteUser;
@BindView(R.id.buttonHideYourPreset)
Button buttonHideYourPreset;
@BindView(R.id.buttonTurnOnNotifs)
Button buttonTurnOnNotifs;

public UserMenuFragment(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView();
}

public UserMenuFragment(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
}

public UserMenuFragment(Context context) {
    super(context);
    initView();
}

private void initView() {
    View view = inflate(getContext(), R.layout.fragment_user_menu, null);
    addView(view);
    bindViewWithButterKnife(this, view);
    onButtonReportUserClicked();
    onButtonBlockUserClicked();
    onButtonMuteUserClicked();
    onButtonHideYourPresetClicked();
    onButtonCopyProfileLinkClicked();
    onButtonTurnOnNotifsClicked();
}

@OnClick(R.id.buttonReportUser)
void onButtonReportUserClicked() {
    Toast.makeText(getContext(), "User Reported!", Toast.LENGTH_SHORT).show();
}

@OnClick(R.id.buttonBlockUser)
void onButtonBlockUserClicked() {
    Toast.makeText(getContext(), "User Blocked!", Toast.LENGTH_SHORT).show();
    changeButtonText(buttonBlockUser, R.string.unblock_user_text);
}

@OnClick(R.id.buttonMuteUser)
void onButtonMuteUserClicked() {
    Toast.makeText(getContext(), "User Muted!", Toast.LENGTH_SHORT).show();
    changeButtonText(buttonMuteUser, R.string.unmute_user_text);
}

@OnClick(R.id.buttonHideYourPreset)
void onButtonHideYourPresetClicked() {
    Toast.makeText(getContext(), "User preset hided!", Toast.LENGTH_SHORT).show();
    buttonHideYourPreset.setText(getContext().getString(R.string.show_your_preset_text));
    changeButtonText(buttonHideYourPreset, R.string.show_your_preset_text);
}

@OnClick(R.id.buttonCopyProfileLink)
void onButtonCopyProfileLinkClicked() {
    Toast.makeText(getContext(), "User's profile link copied!", Toast.LENGTH_SHORT).show();
}

@OnClick(R.id.buttonTurnOnNotifs)
void onButtonTurnOnNotifsClicked() {
    Toast.makeText(getContext(), "User's notification is off now!!", Toast.LENGTH_SHORT).show();
    changeButtonText(buttonTurnOnNotifs, R.string.turn_off_notification_text);
}

private void changeButtonText(Button button, int newText) {
    button.setText(getContext().getString(newText));
}

private void bindViewWithButterKnife(UserMenuFragment whichFragment, View view) {
    ButterKnife.bind(whichFragment, view);
}

}

IMO this is happening because I called the OnClick listeners in my constructor so while view creating(after created) naturally they are running. Could somebody help me to understand where is the exact problem is and how to fix it?

My ButterKnife versions:

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Solution

  • Change initView() to :

    private void initView() {
        View view = inflate(getContext(), R.layout.fragment_user_menu, null);
        addView(view);
        bindViewWithButterKnife(this, view);
    }
    

    I don't think you need to call the methods, the annotations already do the job to set the listeners, right ?