-2

I am relatively new to Android programming and i am struggling to send data between activities.

The application structure that i have at the moment is, a home page that directs to a tab host, that contains activities, and some of the activities have their own tab hosts with their own activities, and then there are screens that are shown and not shown based on user input.

Now the main problem that i am having, is sending information to one of the first level of activities from the lowest level of activities.

At the moment i have one large activity,containing a tab host,managing the inputs for the lower level layouts contained within it.

Is this the best practice, or should i be using another method?

Thanks in advance.

JeanBrand
  • 73
  • 1
  • 1
  • 6

4 Answers4

0

If you find yourself needing to maintain quite a lot of the same information across activities, you can also use the Application class to store global state variables.

http://developer.android.com/reference/android/app/Application.html

Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • You shouldn't do this, what happens when the app dies and all your information is gone? – Kristopher Micinski Jun 19 '13 at 16:30
  • This question is about communication, not storage. The solution above is to facitilate storing variables that you need to access across activities. It has nothing to do with persisting them. Personally I would use a database to persist data. – Ken Wolf Jun 19 '13 at 16:34
  • how is that consistent with the answer you gave, which is completely oriented toward "stor[ing] global state variables?" – Kristopher Micinski Jun 19 '13 at 16:46
  • How is it inconsistent? :) I mean storing them in memory to facilitate communcation between activities, instead of passing around a lot of intents, as the original question is about. Somehat akin to what is described here: http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – Ken Wolf Jun 19 '13 at 16:47
0

You can use putExtra() for purpose of passing simple parameters(not objects) within the intent of the activity being called before starting it, and use it in the called activity with getExtra().

If what you want is sending information to and from activities then you can create an class with static fields corresponding to your needs and then access and modify their values in any of your activities. As for changing values automatically when switching between activities you can override the default functions of your activity to do your work.

You can find the default methods of an activity here. Basically you might need the onPause() and onResume() functions to be overridden

7bluephoenix
  • 958
  • 7
  • 18
  • Is there an event that gets fired on one of the main activities when i use the putExtra() method on one of the lower level activities? – JeanBrand Jun 19 '13 at 11:34
0

To pass data between activities you use Intents. You can add data to an Intent with putExtra() like 7bluephoenix said. To get the data, yo have to configure in your receiving activity a Broadcast Receiver:

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

http://developer.android.com/reference/android/content/Intent.html

Community
  • 1
  • 1
Estefanía
  • 119
  • 2
  • 3
0

The way that my application is structured, I had to use the application context to store values, much like a Singleton approach.

JeanBrand
  • 73
  • 1
  • 1
  • 6