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 6624099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:36:42+00:00 2026-05-25T21:36:42+00:00

I have been reading up on the UI updating when I found out that,

  • 0

I have been reading up on the UI updating when I found out that, like WinForms, Android need to do UI updates on from the main thread (too bad, I was hoping someone could once and for all solve that irritating problem).

Anyways, I need to pass that to the UI thread. Some suggest the use of the runOnUiThread method and that might have worked, if it wasn’t for that irritating “final” that then came into play.

I have an Interface, with a method I cannot change. It looks like this:

@Override
public void connStateChange(ClientHandler clientHandler)

That method is called when I get a change in the connection status (network). When I do, I need to do some stuff, and in this case it is adding some text to a TextView.

So I tried this, but of course the variable “clientHandler” isn’t final:

@Override
public void connStateChange(ClientHandler clientHandler) {

   runOnUiThread(new Runnable()
   {
      public void run()
      {
        tv.append(clientHandler.getIP() + ", " + clientHandler.state.toString() + "\n");
        if (clientHandler.state == State.Connected)
        {
            tv.append("Loginserver hittad");
        }
      }
   });
}

How do I in a nice, clean and efficient way write code in this example to update the UI? I need access to all my variables etc…

  • 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-25T21:36:43+00:00Added an answer on May 25, 2026 at 9:36 pm

    Try it:

    @Override 
    public void connStateChange(ClientHandler clientHandler) {
        final ClientHandler temporaryHander = clientHandler;
        runOnUiThread(new Runnable() {
             public void run() {
                   tv.append(temporaryHandler.getIP() + ", " + temporaryHandler.state.toString() + "\n"); 
                   if (temporaryHandler.state == State.Connected) {
                        tv.append("Loginserver hittad");         
                   }       
             }    
        }); 
    } 
    

    By the way, code becomes more readable, if you declare not anonim class right in the method, but declare inner class out of methods. Consider it as pattern Command.

    Example of more clean and reusable code.

    @Override 
    public void connStateChange(ClientHandler clientHandler) {
        final ClientHandler temporaryHander = clientHandler;
        runOnUiThread(new MyRunnableCommand(temporaryHandler)); 
    } 
    
    private class MyRunnableCommand implements Runnable { 
    
         private ClientHandler clientHandler;
    
         public MyRunnableCommand(ClientHandler clientHandler) {
             this.clientHandler = clientHandler;
         }
    
         @Override
         public void run() {
                   tv.append(clientHandler.getIP() + ", " + clientHandler.state.toString() + "\n"); 
                   if (clientHandler.state == State.Connected) {
                        tv.append("Loginserver hittad");         
                   }       
             } 
    
    }
    

    Though the Runnable implementation itself was inflated a little, code became more re-usable and easy to read.

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

Sidebar

Related Questions

I have been reading up on the changes that .NET4.5 will bring, and on
I have been reading John R. Levine's Linkers and Loaders and I read that
I have been reading the web and trying out things for days looking for
I have been reading through the Java tutorial on RMI. I like the approach
I have been reading up on this, and it seems that if you use
I have been reading Google's android tutorial and I got a problem... In the
I have been reading many of the topics from this search so I really
Have been reading about async and tasks and been attempting to convert the CopyFileEx
I have been reading up on the c++ auto_ptr and unique_ptr and stuff and
I have been reading Rafactoring by Martin Fowler and in the beginning of the

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.