Link to home
Start Free TrialLog in
Avatar of roy_sanu
roy_sanuFlag for India

asked on

how to hold the previous value used in android

can somebody tell me how to hold the previous string value in the current session in android



I have a scenario where i pass a string on the click event, i need to keep that id for the doing some other operation in the current session
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

You can make a global class which can have "Getters" and "Setters" to Get and Set a String by extending the Application class.

Read Section 2. By Extending the Application class
http://androidresearch.wordpress.com/2012/03/22/defining-global-variables-in-android/


You can also pass a Bundle to an Activity, which is a method to pass variables between Activities.
http://miragemiko.wordpress.com/2010/03/20/android-using-bundle-for-sharing-variables/
Avatar of roy_sanu

ASKER

my question if i want to hold the same string value  for the next session how i can do that.

for example

"Let say i had a text and a button. i passed some value and pressed the click button it gave me some output, now i need to hold that immediate previous value to do some other operation or validation, let me know how to do that"
Avatar of Ess Kay
you can, write it to a file
or write to xml
or write to database

then retrieve it when you need it
or if its just from a function, make the function return something

here is psudocide



int x = 5
int y =  functionbla (x)

[now you have two values!! YAY]
roy_sanu,

Before you change the value, you need to put the current value in a String variable. This could be at the top of your click event before the text is changed. Be careful of the scope of the String variable. In other words if you declare it in the click event, then its scope is only in the click event. If you declare it as a Class variable, then it is global to the Class.

Assuming the text box has the value that you want to save and it's an EditText named editText1:

//Find the textbox
EditText etxt = (EditText) this.findViewById(R.id.editText1);                   
//Save the current text value in currentValue
String currentValue = etxt.getText().toString();
ASKER CERTIFIED SOLUTION
Avatar of roy_sanu
roy_sanu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Holding the previous session using preferences of android  is the  best  solution