2

I am having an issue keeping information in my first class after switching to my second class, it basically looks like this.

[int] a = 25;
Intent asd = new Intent(getApplicationContext(), secondActivity.class);
startActivity(asd);

But my issue happens when I start an Intent in the second activity to return to the first class, like so:

Intent jkl = new Intent(getApplicationContext(), firstActivity.class);
startActivity(jkl);

Because when I return to that class, int a is returned to it's initial value, not the value that I modified before I switched classes. I set 'a's value to 20 in the class scope then I change it to 25 in an onClickListener in the onResume() scope.

Am I switching classes the wrong way? Why is the value not saving? It would make sense to me that it shouldn't reset the value to it's initial value since it's initial value is not set in an onStart()/onCreate() method.

NewBee Developer
  • 432
  • 2
  • 9
  • 26
kaio37k
  • 150
  • 1
  • 8
  • please first check life cycle of class and activity... variable initialization,Scope !!!!! you will automatically get your answer – koutuk Dec 23 '15 at 04:49
  • You want to save data of previous Activity use Sharedprefrence – koutuk Dec 23 '15 at 04:50
  • @koutuk Why would I need to save data if it's value isn't reset in onStart or onCreate? – kaio37k Dec 23 '15 at 04:52
  • Possible duplicate of [Sending data back to the Main Activity in android](http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android) – Raviprakash Dec 23 '15 at 04:52
  • Class Load via Top to bottom approach – koutuk Dec 23 '15 at 04:53
  • @Raviprakash It is not the same situation. He is trying to send info *back* to the main class, I am trying to keep modified info in the main class the same after I switch to another class. – kaio37k Dec 23 '15 at 04:57
  • May be it is creating new instance of activity, see http://stackoverflow.com/questions/6706645/single-instance-of-activity – Raviprakash Dec 23 '15 at 05:00
  • @Raviprakash Still not the same thing. For me, Class A is manipulating an int so that the int increases in value. When I switch to Class B (which does not touch anything from Class A) then switch back to Class A, the int's value is not what I set it to before I switch to Class B. – kaio37k Dec 23 '15 at 05:07
  • 1
    You're not switching back to class A, you're starting a *new* class A. If you want to return to the previous activity, call `finish()`. – Kevin Krumwiede Dec 23 '15 at 05:20
  • Create a third class make a variable static public ... and use that variable in your activity ... you will never loose your data until app close – koutuk Dec 23 '15 at 05:28
  • @KevinKrumwiede This is what it was, thank you! I was not familiar with finish() and using has solved my problem! – kaio37k Dec 23 '15 at 05:35

2 Answers2

0

There are plenty of options for storing data while switching between activities, I'll list a couple of them here,

  • You can store your values in shared preferences

  • You can store your values in sqlite.

The first option is used when the values you want to store are key value pairs, and not complex objects. Sometimes people store complex objects by converting the objects to json string.

But sqlite is the preferred way to store complex objects especially with an ORM framework like greenDao, ORMlite etc. as you can directly store and retrieve java objects to and from the sqlite DB using these libraries.

Sharedpreference storage saves key value pairs in files, you can provide the MODE for these files, if you specify private mode then the file will only be accessible to your application. If you want to make the file accessible to other applications you need to specify as public.

Sqlite on the other hand is a lite version of SQL based database storage designed specially for mobile devices. They are supposed to be faster and a lighter version of SQL used in servers.

I'll explain here how you can use shared preferences for your particular situation

SharedPreference

First you can create a sharedPreferencefile using a context object like so

SharedPreferences mySharedPref =  
                    context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);

Here while creating you specify the ACCESS MODE for the file.

Next in order to store values in this file you get a SharedPrefences.Editor object for the file by calling the edit() function on the sharedPreference object.

SharedPreferences.Editor editor = mySharedPref.edit();

Now you can add your int a = 25 key value pair into the editor like so.

editor.putInt("a", a);

Then you need to call the apply() function in order to write to file.

editor.apply();

Then to retrieve all you need to do is call getInt() function on the sharedPreference object.

SharedPreferences mySharedPref = context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE);
mySharedPref.getInt("a",0);

The 2nd argument in getInt() function is a default value in case there was no value for the key "a" in the file.

So what you can do is store the value of a in a sharedpreffile in the onStop() of your activity and retrieve it again in the onStart() of your activity

Bhargav
  • 8,118
  • 6
  • 40
  • 63
0

when you jump to firstActivity by your codes

Intent jkl = new Intent(getApplicationContext(), firstActivity.class);
startActivity(jkl);

it is not go back to firstActivity,these codes create a new firstActivity!so the parameters will be it's initial value.you can just call finish()to close your secondActivity.

  • never use `startActivity()` to back to previous Activity,by this way,your second activity will not recycled,your activity stack will be like this firstActivity->seconedActivity->firstActivity – chichiangho Dec 23 '15 at 05:27
  • This is what did it! Thanks for the help! So to clarify, do you mean that by starting 'asd' through an Intent in the class 'jkl', I am opening a second instance of 'asd' instead of the original one that I am trying to access? – kaio37k Dec 23 '15 at 05:36
  • @kaio37k yes,you can also google launchmode,if your activity's launchmode is singleTop ,singleInstance or singleTask,it will not create a second instance,but if your launch mode is standard(the default one),it will create a second one. – chichiangho Dec 23 '15 at 05:42