QUuestion about mTrueButton and mFalseButton


#1

Hello guys,

I was wondering why those two members(mTrueButton & mFalseButton ) of Activity are needed. Since what has to be done is to bind listener to the button, just using findViewById() to get the reference of those two button is enough.

final Button mFalseButton = (Button)findViewByID(R.id.false_button);
mFalseButton…setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT).show(); }
});

Anybody has any idea about that?


#2

You’re right, those buttons aren’t referenced outside of the onCreate method, so local vars would suffice, however, it’s possible that those buttons might need to be accessed from other methods within the Activity at a later time to either hide or disable them depending on circumstances – hypothetically. It’s just a matter of convenience to make them fields early on. Can always refactor to make them local once it’s clear they won’t be needed elsewhere.


#3

I see. Thanks