Search code examples
javaandroidandroid-intentextras

Passing extras from tab activity android


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


Solution

  • 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.