0

How should I go about sending a message to an activity that is in a tab?

Here's my setup:

I have my main activity that has multiple tabs each with their own instance of the same activity. When the user brings up a context menu and chooses an option I need to send a message to the current tab's activity telling it what to do. I've had very little experience talking between activities. Are broadcastreceivers the correct path to take?

Please point me in the direction I should go.

bwoogie
  • 4,339
  • 12
  • 39
  • 72

1 Answers1

0

It depends on what you're trying to do. If you simply want to launch a new activity and pass in some information to it, then you need to do something like:

Intent i = new Intent(this, SomeOtherActivity.class);
i.putExtra(string, DATA); 
startActivity(i);

where data can be anything listed here: http://developer.android.com/reference/android/content/Intent.html under the putExtra methods.

A BroadcastReceiver is not made for simply passing data between Activities. A BroadcastReceiver is a different idea altogether, the idea being that the class that extends BroadcastReceiver simply lies in wait for a broadcast (via sendBroadcast) and then responds to that broadcast and any data passed to it, if any. But if you need to display an Activity, then you need...err.. an Activity.

Inside of the new Activity, you can grab hold of the extras sent to it via

getIntent()

And then grab whatever extra types you specified when you putExtra initially. Again, see http://developer.android.com/reference/android/content/Intent.html

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • Ok, thats what I was thinking about the broadcasterreceiver... But my activies have already been started. how can I "putExtra" after the activity has already been started? – bwoogie Nov 22 '11 at 03:42
  • If you're using the old Tabhost with ActivityGroup combo (which has been deprecated btw), you can try this solution: http://stackoverflow.com/questions/1306689/launching-activities-within-a-tab-in-android – LuxuryMode Nov 22 '11 at 03:44
  • @bwoogie without using ActivityGroup (or managing an Activity stack some other way), it's not possible, since, as you've said, the Activities are already started. Maybe if you explain exactly what you're trying to do in specific, I can provide more help. – LuxuryMode Nov 22 '11 at 03:51
  • So basically my app is a text editor. you open new documents in a tab. each tab contains an activity which is the editor itself. when you long press on a tab (this is handled by the main activity holding the tabhost) it opens a context menu with your basic file options: new, open, save etc. if, say, the user clicks on 'new' I need to tell the corresponding activity to create a new document. Sure I can move this functionality into the editor's activity itself but I like the implementation of longpressings the tab... – bwoogie Nov 22 '11 at 03:56
  • Ok so heres another direction im willing to go, tell me if its possible. I will instead of longpressing the tab give the activity its own menu through the standard menu system. Now if the parent activity has a menu also will these 2 menus combine? (Im not at my desk right now to try) – bwoogie Nov 22 '11 at 13:54
  • Oh. So then what happens if both have a menu? Which one takes over? – bwoogie Nov 22 '11 at 14:35
  • Whichever Activity is currently at the forefront will be the one that handles any menu keys because its callbacks for the menu button will be called. – LuxuryMode Nov 22 '11 at 14:36