Robert
Robert

Reputation: 1117

Saving numbers in activity to use again later

In the last activity of my app there is some numbers displayed in edit text's and an option to add more to these totals, the add more button takes you back to start of the app, where the user does the same thing again inputs some numbers and ends up back on last activity.

What I want to do is if they press the add more button is save the numbers that are in the edit text's so I can add them to the new set of numbers coming in.

What I thinking is save the numbers in on Pause, but don't know how and then add them to new numbers in on Resume, am I on the right track, what is the best way to do this.

Upvotes: 2

Views: 141

Answers (5)

Anila
Anila

Reputation: 1136

You can use Intent to do so. For example if you want to transfer an int.

  //first activity
  Intent intent =  new Intent(this, FirstActivity.class);
  intent.putExtra("myInt", number);

  //Second Activity
  Intent intent =  this.getIntent();
  int number = intent.getExtra("myInt", -1);

Upvotes: 2

Chintan Soni
Chintan Soni

Reputation: 25267

In the "source" activity do this:

SharedPreferences settings = context.getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);

SharedPreferences.Editor editor = settings.edit();

editor.putString("key", value);
editor.commit();

In the target activity do this:

SharedPreferences preferences = getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);

String value= preferences.getString("key", "");

The second parameter in getString is the default value you want returned if "key" isn't found.

Also Check these links

Android Using SharedPreferences to save and retrieve values - Counter

http://android-er.blogspot.in/2011/01/use-getsharedpreferences-to-retrieve.html

Upvotes: 2

Alex
Alex

Reputation: 1416

you'd better use SharedPreferences.... http://developer.android.com/guide/topics/data/data-storage.html#pref

Upvotes: 1

Vinay
Vinay

Reputation: 2415

Try implementing onSaveInsance and onRestoreInstance of the activity. This would be called before onPause and before onResume.

Sample is here.

Also, to add the data from a different activity which would return to this activity then you can use the startActivityForResult and send the data as intent from the new activity before finishing..

Upvotes: 1

Eugene Popovich
Eugene Popovich

Reputation: 3473

You can use startActivityForResult and send your data via Intent.

Upvotes: -1

Related Questions