0

I have just started android development 3 days ago and I'm trying to develop a messenger for Yahoo. Hell of a first application, right?! :D

The problem I'm facing right now is that I'm trying to implement a tabbed IM window which is supposed to show people that I'm currently chatting with. I designed a layout file and the necessary activity class for a single chat window and I am using a TabActivity to show several of those to the user. However the problem is that I am storing some chat-specific information (such as the ID of the user I am currently chatting with) in the activity class itself and I am facing problems initializing those values when a new tab is created.

Initially I used a BroadcastReceiver and an intent to initialize like so:

protected class MyListener extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(
                    "com.sirm2x.messenger.CHAT_WINDOW_CREATED"))
            {
                 ChatWindowActivity.this.friendId = intent.getExtras()
                 .getString("friendId");
            }
        }
    }

The problem is that the friendId of all the activities are set to the value passed for the last tab-to-be-created!

What is the solution/best practice for situations like this? Am I even approaching the problem correctly?

Any help is hugely appreciated!

Maghoumi
  • 3,295
  • 3
  • 33
  • 49

1 Answers1

1

The problem here is that you register a Broadcast receiver in each or your tabs, and each of your receiver receives the broadcast, setting the friendId variable in it.

I am not quite sure BroadcastReceiver is the correct approach here.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • 1
    if i am right, this broadcast is sent when the chat window is created, which probably means that the parameter can be put in the intent used to start the activity – njzk2 Dec 06 '11 at 13:29
  • That will fix a part of my problem, but what if should I do in case I wanted to initialize other properties and avoid serializing data and send them via intent? Any suggestions? Even better, is there a way to access the fields defined in the activity via the TabActivity? It is very crucial for what I want to do next! – Maghoumi Dec 06 '11 at 13:41
  • you can access some fields statically, you can use more specific intents, you can use Services and Binders – njzk2 Dec 06 '11 at 15:44