1

My problem is as follows:

  • I have an end point, suppose something.com/token, to which I have to hit a GET request. From the response that I obtain, I would like to store it in some String token(This has to be done only once when I start my app)

  • I want to hit another point, suppose something.com/chat, to which I have to pass my token to. In my layout, when I click a button - I want to getText from the EditText and call an Async task to which I will pass my text and token

I have been able to reach a stage in which I am able to hit both the requests multiple number of times. Hence, getting the token multiple times and passing it to my something.com/chat every time I click on the button

How do I call the first step only once and pass it to my /chat implementation

Bhargav Panth
  • 301
  • 1
  • 3
  • 10

2 Answers2

3

You can do this, by using shared preferences. Store a value in shared preferences:

SharedPreferences prefs  = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("key",1); //or you can also use editor.putString("key","value");
editor.commit();

After doing so, say for example if user recalls an activity, then you check the value of in the shared prefs and if it is found, then just perform the action you wish to do else, allow the user to continue with the activity.

To retrieve values from a shared preferences file, call methods such as getBoolean() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn't present.

Here is a quick reference:
http://developer.android.com/reference/android/content/SharedPreferences.html

Panda
  • 2,400
  • 3
  • 25
  • 35
1
if(!alreadyExecuted) {
    doTrick();
    alreadyExecuted = true;
}
Basheer Adel
  • 41
  • 1
  • 5