Best practice to access Activity from Views
<p>I have a question that seems simple but I cannot figure out what is the best practice for that :)</p>

<p>What is the best practice to access from a <strong>View</strong>, a method on the <strong>Activity</strong> that launched the <strong>View</strong>?</p>

<p>For example, I have an Activity with a layout that contains a <strong>Button</strong> and a <strong>Textfield</strong>. I want when I click on the <strong>Button</strong>, to call a method on my <strong>Activity</strong> that update the <strong>Textfield</strong> with some value. I come with multiple solutions:</p>

<p>1 - Inner class for the <strong>OnClickListener</strong> directly on the <strong>Activity</strong> so I can the method of the <strong>Activity</strong> with <strong>MyActivity.this.updateTextField()</strong> on <strong>onClick</strong> method</p>

<p>2 - Outer class for the <strong>OnClickListener</strong>, on my <strong>onClick</strong> method I can do: <strong>((MyActivity)getContext()).updateTextField()</strong></p>

<p>3 - Reference the <strong>Activity</strong> on my <strong>OnClickListener</strong> class when I instantiate it:
<strong>myButton.setOnClickListener(new MyOnclickListener(MyActivity));</strong></p>

<p>I don´t want solution 1 because I don´t like that much inner class and I want reusable code. Solution 2 seems good but can produce error on runtime if my context is not an activity. Solution 3 seems good also but "heavy".</p>

<p>What is the best practice on Android to tell from the <strong>View</strong> to its <strong>Actitity</strong> that something needs to be done on the Activity?</p>

<p>Thanks!</p>
