Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:54:37+00:00 2026-05-31T14:54:37+00:00

I have a question that seems simple but I cannot figure out what is

  • 0

I have a question that seems simple but I cannot figure out what is the best practice for that 🙂

What is the best practice to access from a View, a method on the Activity that launched the View?

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

1 – Inner class for the OnClickListener directly on the Activity so I can the method of the Activity with MyActivity.this.updateTextField() on onClick method

2 – Outer class for the OnClickListener, on my onClick method I can do: ((MyActivity)getContext()).updateTextField()

3 – Reference the Activity on my OnClickListener class when I instantiate it:
myButton.setOnClickListener(new MyOnclickListener(MyActivity));

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”.

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

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:54:38+00:00Added an answer on May 31, 2026 at 2:54 pm

    Although I mostly find myself end up with inner classes, there are other options.
    You can create an interface like the following and let your activity implement it:

    public interface UpdateableTextField {
        public void updateTextField();
    }
    

    Now let the Activities that you want implement this interface.

    Now, create a class that implements View.OnclickListener and set the constructor to get UpdateableTextField as a parameter:

    public class MyListener implements View.OnclickListener {
        UpdateableTextField updatable;
    
        public MyListener(UpdateableTextField updatable) {
            this.updateable = updatable;
        }
    
        @Override public void onClick(View v) {
            // do some stuff
            updateable.updateTextField();
        }
    }
    

    And last, in the Activity:

    public class MyActivity extends Activity implementes UpdateableTextField{
        @Override public void onCreate(Bundle savedInstanceState) {
            // usuall stuff
            MyListener listener = new MyListener(this);
            someView.setOnClickListener(listener);
            // other stuff
        }
    
        @Override public void updateTextField() {
            // well, update the text field :)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple question that I cannot seem to find the answer
A question that seems to have quite a few options for Python, but none
Hopefully have a simple question here but I cannot for the life of me
I apologize if this is a newbie question, but I cannot figure out why
So I'm learning java, and I have a question. It seems that the types
I have a question that may be quite naive, but I feel the need
I have a question that I'm ashamed to ask, but I'm going to have
I have a simple question that I am posing mostly for my curiousity. What
It seems like it should be so simple, but I simply cannot get this
The question seems relatively simple, what I want is that when the user goes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.