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

  • Home
  • SEARCH
  • 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 9216621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:27:26+00:00 2026-06-18T02:27:26+00:00

I write service that interacts with other apps. It registers listeners on views (buttons,

  • 0

I write service that interacts with other apps. It registers listeners on views (buttons, textviews,…), that already have listeners. I need to replace them with my own listeners (works), do some stuff and then unregister my listeners and restore the old ones.

  1. An App with a button with an onClickListener is running
  2. My service registers an onClickListener inside the UI-Thread + do something
  3. My service restores the old listener

It would be easy, if there was a view.getOnClickListener -method. Then I could save the old ones and replace the new listeners when I’m done.

Is there any way to get listeners from a view or have more that one listener of the same type bound to one view?

Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //do something
        }
    });
// I need to do, but found no solution for that.
View.OnClickListener oldListener = btn.getOnClickListener();

If I register the new listeners to a view, the old ones are overridden, right? It would be also okay if both listeners (“new” and “old”) exist at the same time. Only the old ones must not be gone.

edit: Unfortunately I have no possibility to save the listener on assignment. I need to get it backwards from the view component.

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-06-18T02:27:28+00:00Added an answer on June 18, 2026 at 2:27 am

    Thanks to mihail’s hint (thanks for that :)) )with the hidden API, I’ve found a solution to get a listener back after assignment:

    The android.view.View class has a nested class static class ListenerInfo that stores all listeners on a View (API 14+). In older versions the listeners are private fields in the android.view.View.

    The field can be accessed with reflection. In my case (API 14+),

    // get the nested class `android.view.View$ListenerInfo`
    Field listenerInfoField = null;
    listenerInfoField = Class.forName("android.view.View").getDeclaredField("mListenerInfo");
    if (listenerInfoField != null) {
        listenerInfoField.setAccessible(true);
    }
    Object myLiObject = null;
    myLiObject = listenerInfoField.get(myViewObj);
    
    // get the field mOnClickListener, that holds the listener and cast it to a listener
    Field listenerField = null;
    listenerField = Class.forName("android.view.View$ListenerInfo").getDeclaredField("mOnClickListener")
    if (listenerField != null && myLiObject != null) {
        View.OnClickListener myListener = (View.OnClickListener) listenerField.get(myLiObject);
    }
    

    After that code (I missed a lot of try-catch-blocks), the myListener object holds the instance of the onClickListener, that has been anonymously declared to the view before. It also works with any other listener, just replace the “mOnClickListener parameter” with the one you need in the reflection and cast it correctly.

    Note that code changes in upcoming versions can make that not working anymore.

    Found the final tutorial here: http://andwise.net/?p=161

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a module that sends order data to an epayment service,
I'm struggling to write a Windows Service that accesses a website, logs in using
Trying to write app for service technicians that will display open service calls within
How can I write a WCF web service that has single endpoint but multiple
I'm reading a web service that returns a JSON, parse and write into a
I've been tasked to write a high performance, high availability service that will run
In short: Trying to write a wcf service for a winform-app that invokes a
I'm trying to write a library that will register an arbitrary list of service
We have 1000 threads that hit a web service and time how long the
Goal: Provide a web service using Visual Basic or C# or .NET that interacts

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.