2

I am going crazy trying to figure out how to store a variable someplace so when I switch to another activity then back again the variable still contains this value. I did this a while ago using shared preferences, but this was a bad solution, this was way more permanent than I needed and just not the correct way.

If a certain button is pressed in activity one, this opens activity two via an intent and sets a value to a string in activity two. When going to another activity, or pressing the back button, then returning to activity two, the string is reset back to its initial value.

I have tried:

Shared Preferences (Worked but not good) Static variables (Did not seem to make any difference, maybe I am doing something wrong) Using saved instance state and restore state methods like so:

 @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
     savedInstanceState.putString(destination, des);
        super.onSaveInstanceState(savedInstanceState);
    }


 @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        des = savedInstanceState.getString(destination);
    }

I am at a point where I admit I have no idea how to do this, and Frankensteining code together is causing me problems than anything else, but so far no pitchforks on the horizon.

In order to save a value inside a string between activities, what do you experienced programmers recommend?

deucalion0
  • 2,422
  • 9
  • 55
  • 99
  • 1
    I don't know why people downvote so quickly sometimes. Very irritating. For future downvoters, if we do this when people are showing real effort then we will lose the site because people will be afraid to ask for help. There, that's my 2 cents for the day :P – codeMagic Mar 14 '13 at 02:18
  • Thanks codeMagic, and I have to say that there are a lot of times I write a question then delete it because there is nothing worse than a downvote, especially when you are at the stage where you need to ask for help. – deucalion0 Mar 14 '13 at 09:48

2 Answers2

3

You can just pass it as an extra using Intent

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("key", value); // where key is whatever you want to name it 
                              //and will be used to retrieve it in the next 
                              //activity and value is the actual value of your string
startActivity(intent);

Then get the value in your Second Activity

Intent curIntent = getIntent();
String variableName = curIntent.getStringExtra("key");

Intents

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I understand this will pass a value to my string variable, it is keeping it there when I leave the activity which is the problem. But your code is a better way to pass a value than the way i was doing it! :) – deucalion0 Mar 14 '13 at 00:27
  • 1
    I'm glad you found it helpful. We do have a "Globals" class with static variables but I don't like to use that any more than I have to. Actually trying to clean it out – codeMagic Mar 14 '13 at 00:29
  • codeMagic, I wanted to ask you, are you a developer who may be interested in helping me solve a small issue with my Android application as a job? The job will probably take you 10 minutes to do, what I couldn't in 3 weeks. :) my email is deucalion0@gmail.com – deucalion0 Mar 14 '13 at 21:49
  • and now to get back the value in the first activity ?? – rainman Oct 14 '16 at 09:20
  • @rainman see this answer http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541 – codeMagic Oct 15 '16 at 18:38
1

Another option is to extend Activity with a new class, say called SuperActivity. Then, in each of your subsequent activity classes, extend the SuperActivity class. Place the items you would like to be globally accessible in the SuperActivity class.

Keep in mind that this would build the SuperActivity class with each new Activity - so a dynamic variable would need to be initialized. However, static variables would be maintained from implementation to implementation.

For what it's worth, I wouldn't use this to replace passing data via intents. By doing this, every activity has every variable contained in SuperActivity. So unless the variable is required in all (or nearly all) of your activities, it's better to pass it via intent.

Tim C
  • 635
  • 5
  • 18