Play with Data

Friday 28 September 2012 0 comments

Data Transaction and Application Variable


Android gives its developers to do programming in an effective way. On this way its gives us to maintain a state of a variable throught the application. But this have few disadvantages of using it in all situation.

Via Intents


If your requirement is to get the data only to next activity, then go with Intents. Pass the value with the Intents while starting the Activity using Key-Value set. Get the value from the Intent passed from the previous Activity and store it in a Bundle. Retrieve the value with the key set for the value you need.



 While retrieving,


Shared preferences


If your case is to store a value in an Activity and it will be used in some other Activity which is not next to it or it will be shared my more activities, then you can go with Shared preferences. 



 While retrieving,

Application Class


If you have to several data need to be passed and will be used in various Activities, then the best advise is to go with the Application class. Simply getter-setter class. For this to work, you have to create a class which should extends Application.

Then declare the variables you need to pass data between activities. After declaring, create getter methods and setter methods. If you are using Eclipse, it will do the job for you. All you have to do is, create a class which extends Application and the data variables. After that go to "Source Tab" in eclipse, select "Generate Getters and Setters". It will pop you a window which will ask to check the variables for which to generate getter and setter methods. Check the needed ones and click Ok. It'll create the required methods for you. Do not use this method to get and set values inside the same class.



While using this Class, do declare it in manifest file. While using, create an object for this class with the Application context and get the state of the data.




Happy Coding :)








0 comments:

Post a Comment

 

©Copyright 2011 Bleed in Android | TNB