Junaid Noor
Junaid Noor

Reputation: 474

accessing UI elements of activities from another class

Ok first of all android is really confusing. The scenario is I have about two runnable classes which are created from a Login View and if logged in it will create another view which will have other data and even more activities can be created from there

Now I can pass the the Login view context when creating a thread for the runnable class and edit out UI elements in them like this:

((Activity)someContext).runOnUiThread(new Runnable(){
    public void run()
    {
        TextView txtErr = (TextView) ((Activity)someContext).findViewById(R.id.errMsg);
        txtErr.setText("Some message");
    } 
});

But the issue is there will be more activities that will be created and the runnable class is created at the time of logging in, and I can't keep passing contexts.

Is there a better way for accessing the UI elements of different activities from different threads?

P.S: the threads which will be accessing the UI elements doesn't extend Activity and are running in a separate thread.

EDIT

I think I need to make my question more clear... I am developing a client app for a messenger... The process goes this way... User clicks on login button which creates a thread in a separate class named ClientThread for handling socket connection and keeping the connection alive till the user logs out or connection drops. The ClientThread class loops till the socket is connected and whenever some data is received the data is passed to another thread in a class named ProcessDataThread which do the parsing of data and will update the UI accordingly.

Now in a response from server if the user is logged in I want to create an activity from that class and keep a context to that activity in ProcessDataThread as I will be updating UI on further responses from server. And if login fails ProcessDataThread will display a message on the main activity saying login failed, now I was able to achieve the later by passing the context from the MainActivity to the two threads when clicked on Login like this:

global_constants.clientObject = new ClientThread(this);
global_constants.clientThread = new Thread(global_constants.clientObject);
global_constants.clientThread.start();

And then from ClientThread to ProcessDataThread

global_constants.updateConversationHandler.post(new ProcessDataThread(SharedBuff, cntxt));

But how will I create more activities from a non-activity class and do all update them or find a UI element etc...

Upvotes: 0

Views: 1667

Answers (1)

Eliot
Eliot

Reputation: 5659

Not sure if I understand you, but it sounds like you are trying to control the view of an activity from outside of the Activity. This sounds hacky to me. I'd let each Activity manage its own UI.

A good way of doing decoupled communication between objects is the observer pattern, aka an "event bus" or "event dispatcher" system. An example of how to do this on Android is here: http://www.therealjoshua.com/2012/03/event-dispatching-sending-messages/

Basically, the code that's generating the error should dispatch a message. The Activity can listen for this message, and then update its own UI as needed.

EDIT

Thanks for the clarification. I think the observer pattern can still help here. Basically, your data processing threads shouldn't know anything about the UI. Just have them post an event for the error, optionally with additional info on the error. If you want, your event dispatcher class could even make the actual event calls on the UI thread itself using a Runnable like you showed, so that the listener can always assume that they are being called on the UI thread, if this is important for your design. This way you don't have to pass the context to the thread at all (at least not for purposes of updating the UI) - let the worker thread just be responsible for the work, and the activity can be responsible for its own UI.


Another option you could use is an android Handler (see http://developer.android.com/reference/android/os/Handler.html) In this case, the work is still done in another thread, but the Activity receives a handleMessage callback from the thread at the appropriate time. I haven't used this myself but from the documentation it looks like it can get the job done for what you need.

In either case IMO, the responsibility for updating the UI should lie with the Activity, not the worker thread.

Upvotes: 1

Related Questions