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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:19:21+00:00 2026-06-09T06:19:21+00:00

I have a MainActivity the default start up Activity which creates a TCP worker

  • 0

I have a MainActivity the default start up Activity which creates a TCP worker thread.
This TCP thread receives some data from the server and passes it to the current Activity on display say there are two more Activities called Activity1 and Activity2 which will display the received data.

How do i achive this using Handlers ?
Here is a outline of what I have…please suggest solution or change everything if I am completely wrong.

public class MainActivity extends Activity
{
    public static TCPFunctions tcp = null;
    public static Handler handler;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler = new Handler();
        tcp = new TCPFunctions(this, handler);
        tcp.start();
    }
}

—————————————-TCP Thread Class———————————

public class TCPFunctions extends Thread
{
    public Handler handler;
    //socket and io streams are here and they work properly

    Public TCPFunctions(MainActivity main, Handler _handler)
    {
        this.main = main;
    handler = _handler;
    }

    public void run()
    {
        Intent showActivity1 = new Intent(main, Activity1.class);
        main.startActivity(showActivity1);
        while(true)
        {
            directories = new Vector<String>();
            directories = (Vector<String>) inputStream.readObject();
            Message msg = Message.obtain();
            msg.obj = directories;
            handler.sendMessage(msg);
            directories = null;
        }
    }
}

now say in Activity1 I want this directories object…
Lets say my Activity1 has a button which when pressed sends a request to the server to get the directories object…which is received by the TCP thread…now how do I get this in Activity1 and update the UI…

Basically the directories object is a Vetor of Strings and I want to display the strings on a ListView contained in the Activity1/Activity2

public class Activity1 extends Activity implements OnClickListener,OnItemClickListener
{
    private ListView directoryList;
    private Button rootButton;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fileexplorer);
        directoryList = (ListView) findViewById(R.id.directories);
        rootButton.setOnClickListener(this);
        directoryList.setOnItemClickListener(this);
    }

    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.root_button:
            Log.d(TAG, "FileExplorer: Root Button Pressed");
            //request for directories made here
            break;
        }
    }
 // What should be the Handler code to get the directories ?
}
  • 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-09T06:19:23+00:00Added an answer on June 9, 2026 at 6:19 am

    First: The handler executes runnables and handles messages in the thread that created it. So if you want Activity1 to react to data from TCPFunctions, you have two options. Either:

    -MainActivity, which created the handler in your current code, needs to react to the message, get the data, and send it along to Activity1

    -Or Activity1 needs to be the one to create the handler.

    In either case, the core answer to your question of how to react to a sent message, is that you need to override the handleMessage() method by subclassing the handler. Here’s a boilerplate snippet you can use (pulled from one of the sample apps on the Android developer website)

            mUpdateHandler = new Handler() {
                @Override
            public void handleMessage(Message msg) {
                String chatLine = msg.getData().getString("msg");
                addChatLine(chatLine);
            }
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this situation where i have to start an activity from my mainActivity.
I have an activity ( MainActivity.java ) in which content view is like this
My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because
I have a Activity called main. If I call Toast.makeText(this, Hello World from main,
I have TabActivityGroup: MainActivity class contain some tab, that name loading from db. Sales,
I have this structure: 1) main activity: public class mainActivity extends Activity { @Override
I have MainActivity which does some Work before it Executes an AsyncTask called Datensammlung.
I have an activity, I'll call it mainactivity for now. And I want to
i have main activity in which i have Four menus. and i have one
I have a design of activities like this I have one main activity and

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.