1

I am having an issue with tabs, and I figure someone will know how this works. Basically I am trying to pass data between tabs in an app, but I am not sure how to do this. I had it set up before where a button would change the activity, but when I moved it to a tabbed view, I lost a way to pass the info through intents like I was doing before. Basically my set up now is:

  1. A TabActivity to switch between my other 2 activities
  2. 2 list activities being connected by the tab activity

It would be awesome if I could somehow pass the info through intents still, but I am not sure if that can work, it would also work if somehow in one of the activities I could access the sharedpreferences from the other. Is there anyway to do either of these? Thanks in advance.

WWaldo

WWaldo
  • 167
  • 3
  • 14

1 Answers1

0

Passing data between Activities which are the contents of Tabs is tricky.

One way that you can try is to us a 'sticky' Intent. Take a look at sendStickyBroadcast - you'll need to have a BroadcastReceiver in each Activity to process the sticky Intent which you can do by defining a private nested class extending BroadcastReceiver.

As far as SharedPreferences are concerned, this can be done at the app level by specifying a filename for the SharedPreferences rather than using Activity-based preferences.

See getSharedPreferences(String name, int mode) - as long as both Activities specify the same filename they will both gain access to the same SharedPreferences file.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Alright, I ended up doing the sharedPreferences thing, thanks for that, I saw that before, but I wasn't sure it worked that way, thanks for clearing that up – WWaldo May 29 '11 at 22:50
  • Actually, I have another question if you see this, when I used the sharedPreferences thing, the list I am saving isn't updated unless I completely close the application, is this something that sharedPreferences just do, not being saved until the app dies? Or is this something I am messing up? – WWaldo May 29 '11 at 23:00
  • @WWaldo: As long as you commit() changes to the SharedPreferences it should update immediately as far as my experience goes. – Squonk May 29 '11 at 23:03
  • Okay, I did that, must be an error else where, I will look, thanks again! – WWaldo May 29 '11 at 23:10