TabActivity showing several tabs of the same activity (Initialize properties on child activities)

StackOverflow https://stackoverflow.com/questions/8400395

  •  28-10-2019
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top